23
我试图做到这一点更新多行3.2
User.find([23,45,68,123]).update_all(:is_active => true)
,但我得到:
NoMethodError: undefined method `update_all' for #<Array:0x00000007493778>
什么是正确的语法?如果我不需要,我宁愿不重复每一个。
我试图做到这一点更新多行3.2
User.find([23,45,68,123]).update_all(:is_active => true)
,但我得到:
NoMethodError: undefined method `update_all' for #<Array:0x00000007493778>
什么是正确的语法?如果我不需要,我宁愿不重复每一个。
find
返回一个数组,因此您不能使用update_all
。
为了解决这个问题,我想你可以使用where
,它返回一个ActiveRecord::Relation
,所以update_all
应该工作:
User.where(:id =>[23,45,68,123]).update_all(:is_active => true)
http://apidock.com/rails/ActiveRecord/Relation/update_all
我希望它可以帮助...
不错, 谢谢;效果不错 – timpone 2013-03-15 00:02:45
很棒的+1 – 2013-07-31 09:32:52