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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With