Check If ActiveRecord Update Fails
There are two ways to update an ActiveRecord instance (not to mention assign_attributes, update_attribute, etc.).
You can call update and update!. If the updates would make the record invalid based on the model's validations, then the update will fail.
You can tell if update failed because it will return false (instead of true).
unless book.update(book_params)
log_book_update_failed(id: book.id)
endThe update! version will raise an ActiveRecord::ActiveRecordError exception if the update fails.
begin
book.update!(book_params)
rescue ActiveRecord::ActiveRecordError
log_book_update_failed(id: book.id)
endLast updated
Was this helpful?