stackPusher
stackPusher

Reputation: 6512

Trigger callback after_commit but NOT as a result of touch

I'm trying to trigger my callback whenever an object is committed. But I don't want the callback to trigger if the after_commit is a result of the object simply being touched. I've looked into after_update , after_update_commit , and after_save, but none of these seem to match the behavior i'm describing. Is there a way to do this other than to have my callback reload the object from the database and verify that a column other than updated_at has been modified before proceeding? I'm on Rails 5.2.

Upvotes: 4

Views: 2598

Answers (1)

stackPusher
stackPusher

Reputation: 6512

Well, in the end the easiest way I found was to bundle the ar_transaction_changes gem. That gem preserves the changes to the record across the transaction and allows me to filter my callbacks like so:

# only kick off job if the update is not due to a touch
after_commit :kickoff_job, on: :update, unless: -> { transaction_changed_attributes.keys == ['updated_at'] }

this way the callback isnt executed if the after_commit is the result of just a touch.

Upvotes: 4

Related Questions