Rails user.update_attribute (:pwd, pwd) 的拆分?

egg_show · 2014年05月28日 · 最后由 yiming 回复于 2014年05月28日 · 2425 次阅读

user.update_attribute(:pwd, pwd) 是这样理解吗: user.pwd = pwd 更新到对象 user.update 更行到数据库 这样两步吗? 而不是直接将 pwd 更新到数据库

数据库里应该没有一个 pwd 字段吧,应该是一个虚拟属性

https://ruby-china.org/topics/2163

def update(attributes)
  # The following transaction covers any possible database side-effects of the
  # attributes assignment. For example, setting the IDs of a child collection.
  with_transaction_returning_status do
    assign_attributes(attributes)
    save
  end
end

Yes, two steps. The last step is save.

#1 楼 @miclle 就是看到了虚拟属性,所以想到更新应该是两步?

update_attribute 不会触发 validations 尽量不要用~

#4 楼 @zj0713001 那用什么,update_attributes?

#5 楼 @egg_show 是的 或者这样写

model.attributes = {}
model.save  # or model.save!
7 楼 已删除

#6 楼 @zj0713001 怎么在 model 里面用 lib 里面定义的 util 呢 - -

#8 楼 @egg_show 可以直接用 config 里 autoload_paths 加上 lib 路径就行了

https://github.com/rails/rails/pull/6738#issuecomment-39584005

根据这个 PR,update_attribute 一度还被移除过。。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号