Read more

How to capture changes in after_commit

Arne Hartherz
July 22, 2015Software engineer at makandra GmbH

Your after_commit callbacks will not know about changes, as Rails discards them when committing.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

The linked article shows a clever trick to work around that: It uses an after_save method that looks at changes and writes its decision to an instance variable. That instance variable can then be used in the after_commit method.

Note that while callbacks like after_save are not affected, there are valid reasons for using only after_commit, and not after_save. Enqueueing a Sidekiq job is just one of them.

Rails 5+

You can use saved_change_to_attribute? and previous_changes[:attribute] in after_commit callbacks.

See ActiveModel::Dirty Show archive.org snapshot

Posted by Arne Hartherz to makandra dev (2015-07-22 12:08)