Just out of curiosity, is there a way to say this...
user.update_column(:field1, true)
user.update_column(:field2, true)
... in one line in Ruby on Rails?
As far as I know an update_columns
method does not exist...
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.
Note: this is only available in ActiveRecord
v4.0.2
and higher:
You can do in this way:
update_columns(field1: value, filed2: value)
what about doing it like this:
user.attributes = attributes
user.save(validate: false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With