Reputation: 426
How can I use update_all for the following purpose:
UPDATE table_name set field1=field2 * 2 where id = 1
Upvotes: 2
Views: 732
Reputation: 29094
In Rails4, you can do
Model.where(id: 1).update_all("field = field2 * 2")
In Previous versions of Rails, you can pass condition to update_all
, like this
Model.update_all("field = field2 * 2", {:id => 1})
NOTE:
update_all does not trigger Active Record callbacks or validations
Upvotes: 5
Reputation: 43
I'm not 100% clear on your intention, but if you're just trying to do a 1 time update of all records that mach id = 1 running this in your console should work.
Model.where("id = 1").each { |n| (n.field1 = n.field2*2)}
Upvotes: 0