How to update multiple columns in Ruby on Rails 3?
You can use update_all
as follows:
User.where(:id => user.id).update_all({:field1 => true, :field2 => true})
This will generate the following update statement (mysql):
UPDATE users SET field1 = 1, field2 = 1 WHERE users.id = <whatever>
Callbacks and validations will not be run.
what about doing it like this:
user.attributes = attributes
user.save(validate: false)
Note: this is only available in ActiveRecord
v4.0.2
and higher:
You can do in this way:
update_columns(field1: value, filed2: value)