Ruby-on-rails 在 Rails 3.2 中更新多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15422021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
update multiple rows in Rails 3.2
提问by timpone
I am trying to do this
我正在尝试这样做
User.find([23,45,68,123]).update_all(:is_active => true)
but I get:
但我得到:
NoMethodError: undefined method `update_all' for #<Array:0x00000007493778>
What would be the correct syntax? I would rather not iterate through each one if I don't have to.
什么是正确的语法?如果不需要,我宁愿不遍历每一个。
回答by gabrielhilal
findreturns an array, so you cannot use update_all.
find返回一个数组,因此您不能使用update_all.
To solve the problem, I think you can use where, which returns an ActiveRecord::Relation, so the update_allshould work:
为了解决这个问题,我认为你可以使用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
http://apidock.com/rails/ActiveRecord/Relation/update_all
I hope it helps...
我希望它有帮助...

