Run a callback only if an attribute has changed in Rails

Ruby on-RailsCallback

Ruby on-Rails Problem Overview


I have the following association in my app:

# Page 
belongs_to :status

I want to run a callback anytime the status_id of a page has changed.

So, if page.status_id goes from 4 to 5, I want to be able to catch that.

How to do so?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails 5.1+

class Page < ActiveRecord::Base
  before_save :do_something, if: :will_save_change_to_status_id?

  private

  def do_something
    # ...
  end
end

The commit that changed ActiveRecord::Dirty is here: https://github.com/rails/rails/commit/16ae3db5a5c6a08383b974ae6c96faac5b4a3c81

Here is a blog post on these changes: https://www.fastruby.io/blog/rails/upgrades/active-record-5-1-api-changes

Here is the summary I made for myself on the changes to ActiveRecord::Dirty in Rails 5.1+:

ActiveRecord::Dirty

https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html

Before Saving (OPTIONAL CHANGE)

After modifying an object and before saving to the database, or within the before_save filter:

  • changes should now be changes_to_save
  • changed? should now be has_changes_to_save?
  • changed should now be changed_attribute_names_to_save
  • <attribute>_change should now be <attribute>_change_to_be_saved
  • <attribute>_changed? should now be will_save_change_to_<attribute>?
  • <attribute>_was should now be <attribute>_in_database
After Saving (BREAKING CHANGE)

After modifying an object and after saving to the database, or within the after_save filter:

  • saved_changes (replaces previous_changes)
  • saved_changes?
  • saved_change_to_<attribute>
  • saved_change_to_<attribute>?
  • <attribute>_before_last_save

Rails <= 5.0

class Page < ActiveRecord::Base
  before_save :do_something, if: :status_id_changed?

  private

  def do_something
    # ...
  end
end

This utilizes the fact that the before_save callback can conditionally execute based on the return value of a method call. The status_id_changed? method comes from ActiveModel::Dirty, which allows us to check if a specific attribute has changed by simply appending _changed? to the attribute name.

When the do_something method should be called is up to your needs. It could be before_save or after_save or any of the defined ActiveRecord::Callbacks.

Solution 2 - Ruby on-Rails

The attribute_changed? is deprecated in Rails 5.1, now just use will_save_change_to_attribute?.

For more information, see this issue.

Solution 3 - Ruby on-Rails

Try this

after_validation :do_something, if: ->(obj){ obj.status_id.present? and obj.status_id_changed? } 

def do_something
 # your code
end

Reference - http://apidock.com/rails/ActiveRecord/Dirty

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHommer SmithView Question on Stackoverflow
Solution 1 - Ruby on-RailspdobbView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMateus LuizView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMonideepView Answer on Stackoverflow