FormAPI Blog

Rails 5.1 ActiveModel::Dirty Changes

You might have seen some deprecation warnings when updating to Rails 5.1+:

DEPRECATION WARNING: The behavior of `attribute_changed?` inside of
after callbacks will be changing in the next version of Rails.
The new return value will reflect the behavior of calling the method
after `save` returned (e.g. the opposite of what it returns now).
To maintain the current behavior, use `saved_change_to_attribute?` instead.

I was getting these warnings inside a before_save callback, so the message was a bit misleading. (saved_change_to_attribute? should be used in a after_save callback, but not in a before_save callback.)

This blog post was really helpful for figuring out what to do with after_save callbacks: Underrated and enhanced dirty attributes changes in Rails 5.1.

However, I couldn’t seem to find anything about before_save callbacks. I read the source code to figure out the new methods, so I decided to put together some tables to show you how you can update your before_save and after_save callbacks for Rails 5.1+. The following examples are for a status attribute:

before_save callbacks

Rails 5.0Rails 5.1+

after_save callbacks

Rails 5.0Rails 5.1+