Rails has multiple methods to update a record,
each one reserved for a unique purpose.
While the traditional update
is used to update records by executing validations,
callbacks
and
track changes, there might be a few instances where one would like to skip validations.
Using update_attribute
does just that.
Before
To skip validations on the update,
use update_attribute
that ensures:
- Validations are skipped
- Callbacks are invoked
- updated_at / updated_on column is updated (if that column is available)
- Tracks changes via ActiveModel::Dirty
Let’s look at an example!
Now let’s compare the differences between update
, update!
and update_attribute
,
As we can see, validations are skipped when using update_attribute
but what is interesting is the behavior of update
and update!
.
While update
silently fails,
calling update!
throws an ActiveRecord::RecordInvalid
error.
Let’s compare callbacks now.
Now update
and update_attribute
silently fail,
while update!
gives us a better understanding.
Unfortunately, there is no equivalent of update!
for update_attribute
that can
give us insights into record updates.
After
Fortunately, Rails introduces update_attribute! which can be used to prevent “silent” updates.
This change adds a new method,
ActiveRecord::Persistence#update_attribute!
which calls save!
instead of save
.
The update_attribute!
method will now raise an ActiveRecord::RecordNotSaved
error should any before_*
callbacks throw :abort
.