Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does after_commit actually run?

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.

like image 455
Austin Avatar asked Feb 24 '23 22:02

Austin


1 Answers

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.

like image 86
dunedain289 Avatar answered Feb 26 '23 23:02

dunedain289