Conditional Callbacks
Rails has a method for triggering a callback conditionally: <column>_changed?
. In the case of my current project, I want to trigger a Slack web hook only if a specific attribute, 'likes', has changed.
# app/models/post.rb
after_update :notify_slack, if: :likes_changed?
Of course, this logic could be moved into the callback method definition, but I like that it's stated upfront at the top of the class.
Tweet