Conor's blog -

🐇

Different Ways to Set Attributes in ActiveRecord (Rails 6)

January 11, 2020

In 2014, David Verhasselt wrote an excellent blogpost on the various methods ActiveRecord provides us to update records, how they differ, and their unique side-effects.

I’ve referenced it many times over the years when reading codebases, and determining the best way to update a particular attribute.

In this post, I’d like to bring his blogpost up-to-date for Rails 6.

Cheat Sheet

MethodDefault AccessorSaved to DBValidationsCallbacksTouches updated_atReadonly check
attribute(s)=YesNo----
write_attributeNoNo----
assign_attributesYesNo----
update_attributeYesYesNoYesYesYes
update_attributesYesYesYesYesYesYes
updateYesYesYesYesYesYes
update_column(s)YesYesNoNoNoYes
User::updateYesYesYesYesYesYes
User::update_allNoYesNoNoNoNo
User::insert(_all)NoYesNoNoNoNo
User::upsert(_all)NoYesNoNoNoNo

As of Rails 6, update_attributes has been deprecated in favour of update.