vwby2a
Last Updated: February 25, 2016
·
604
· devatotech

Update Attributes in ActiveRecord

user.update(name: "Rob")
This method used to be called update_attributes in Rails 3. It changes the attributes of the model, checks the validations, and updates the record in the database if it validates.

Note that just like update_attribute this method also saves other changed attributes to the database.

user.update_columns(name: "Rob")
Much like User::update_all this executes a direct SQL UPDATE query and bypasses any validations or callbacks. It does check first if ony of the columns are marked as readonly and if so, raises an exception.

user.update_column(:name, "Rob")
This is equivalent to calling

user.update_columns(name: "Rob")

described above.

User.update(1, name: "Rob")

A Full Detailed post about this can be found here:
http://www.davidverhasselt.com/set-attributes-in-activerecord/