Is after_commit actually running?

I am using rails 3.0.8 and am trying to use the after_commit callback.

It is defined here: https://github.com/rails/rails/blob/v3.0.8/activerecord/lib/active_record/transactions.rb#L210

It is referred to as one of the callbacks here: https://github.com/rails/rails/blob/v3.0.8/activerecord/lib/active_record/callbacks.rb#L22

Consider this:

class Car < ActiveRecord::Base after_commit do # this doesn't execute end after_commit :please_run def please_run # nor does this end end 

Any ideas why this is not working? I assume that I am using it correctly.

+4
source share
2 answers

If you are experimenting with this in your test suite, you need to set self.use_transactional_fixtures = false for this class. By default, Rails runs a test package inside a transaction and rolls back at the end to clear it. This makes your tests quick, but if you rely on transaction control yourself or this callback, it will not work.

+5
source

Now you can use test_after_commit gem.
The readme says:

"Make after_commit test callbacks for Rails 3+ with transactional_names = true."

https://github.com/grosser/test_after_commit

+3
source

All Articles