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

egg_show · May 28, 2014 · Last by yiming replied at May 28, 2014 · 2426 hits

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 Floor has deleted

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

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

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

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

You need to Sign in before reply, if you don't have an account, please Sign up first.