Posted almost 4 years ago. Visible to the public. Repeats. Linked content.

Different ways to set attributes in ActiveRecord

Rails 3

Method Uses Default Accessor Mass Assignment Protection Saved to Database Validations
attribute= Yes No No n/a
write_attribute No No No n/a
update_attribute Yes No Yes No
attributes= Yes Yes* No n/a
update_attributes Yes Yes Yes Yes

* Mass Assignment Protection for attributes= is overridable.

Further reading: update_attributes vs. update_attribute in ActiveRecord

Rails 4

Method Uses Default Accessor Saved to Database Validations Callbacks Touches updated_at Readonly check
attribute= Yes No n/a n/a n/a n/a
write_attribute No No n/a n/a n/a n/a
update_attribute Yes Yes No Yes Yes Yes
attributes= Yes No n/a n/a n/a n/a
update Yes Yes Yes Yes Yes Yes
update_column No Yes No No No Yes
update_columns No Yes No No No Yes
User.update Yes Yes Yes Yes Yes Yes
User.update_all No Yes No No No No

Rails 5

Method Uses Default Accessor Saves to Database Runs Validations Runs Callbacks Updates updated_at/updated_on Respects Readonly
attribute= Yes No n/a n/a n/a n/a
attributes= Yes No n/a n/a n/a n/a
assign_attributes Yes No n/a n/a n/a n/a
write_attribute No No n/a n/a n/a n/a
[]= No No n/a n/a n/a n/a
update Yes Yes Yes Yes Yes Yes
update_attribute Yes Yes No Yes Yes Yes
update_attributes Yes Yes Yes Yes Yes Yes
update_column Yes Yes No No No Yes
update_columns Yes Yes No No No Yes
User.update Yes Yes Yes Yes Yes Yes
User.update_all No Yes No No No No

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Emanuel De
Last edit:
about 1 year ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more