在Rails 3.2中更新多行

tim*_*one 33 activerecord ruby-on-rails

我想这样做

User.find([23,45,68,123]).update_all(:is_active => true)
Run Code Online (Sandbox Code Playgroud)

但我得到:

NoMethodError: undefined method `update_all' for #<Array:0x00000007493778>
Run Code Online (Sandbox Code Playgroud)

什么是正确的语法?如果我不需要,我宁愿不迭代每一个.

gab*_*lal 72

find返回一个数组,所以你不能使用update_all.

为了解决这个问题,我认为你可以使用where,返回一个ActiveRecord::Relation,所以update_all应该工作:

User.where(:id =>[23,45,68,123]).update_all(:is_active => true)
Run Code Online (Sandbox Code Playgroud)

http://apidock.com/rails/ActiveRecord/Relation/update_all

我希望它有所帮助......