Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 661
stackPusher Avatar asked Oct 27 '25 06:10

stackPusher


1 Answers

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.

like image 182
stackPusher Avatar answered Oct 28 '25 21:10

stackPusher