title: Rails 3: ActiveRecord observer: after_commit callback doesn't fire during tests, but after_save does fire
body: <p>I have a Rails 3 application. I use after_save callbacks for some models and after_commit callbacks for one of the models. All of the code the code works fine, but during RSpec tests, the after_commit callback doesn't get called when I save a <em><code>Thing</code></em> model.</p>
  
  <p>e.g. </p>
  
  <pre><code>class ThingObserver  &lt;  ActiveRecord:Observer
    observe Thing
    def after_commit(thing)
      puts thing.inspect
    end
  end
  </code></pre>
  
  <p>If I change the method name  to <code>after_save</code>, it gets called fine during tests.  I need to be able to use <code>after_commit</code> for this particular model because in some situations the change to a "thing" happens in a web server, but the effect of the observer happens in a Sidekiq worker, and <code>after_save</code> doesn't guarantee that the data was committed and available when the worker is ready for it. </p>
  
  <p>The config for RSpec looks like this in spec/spec_helper.rb</p>
  
  <pre><code>Rspec.configure do |config|
    #yada yada
    config.use_transactional_fixtures = true
    #yada yada
  end
  </code></pre>
  
  <p>I have also adjusted <em><code>rake db:create</code></em> so that it draws from a structure.sql file. In lib/tasks/db.rb</p>
  
  <pre><code>task setup: [ 'test:ensure_environment_is_test', 'db:create', 'db:structure:load', 'db:migrate', 'db:seed' ]
  </code></pre>
  
  <p>I did this so that I could run tests to ensure that the database was enforcing foreign key constraints. </p>
  
  <p>Is there a way to run both the after_save and after_commit callbacks without making the Rspec use_transactional_fixtures == false? </p>
  
  <p>Or, is there a way to set config.use_transactional_fixtures to 'false' just for that test, or for that test file?</p>