4
votes

I'm on rails 3.0.8 and trying to use the after_commit callback.

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

It's mentioned 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 it doesn't work? I assume I'm using it correctly.

2

2 Answers

5
votes

If you're experimenting with this in your test suite, you'll have to set self.use_transactional_fixtures = false for that class. By default, Rails executes a test suite inside a transaction and does a rollback at the end to clean up. It makes your tests fast, but if you rely on controlling transactions yourself or this callback, it doesn't work.

3
votes

You can now use the test_after_commit gem.
The readme says

"Make after_commit callbacks fire in tests for Rails 3+ with transactional_fixtures = true."

https://github.com/grosser/test_after_commit