Threadsafe after_commit callbacks for ActiveRecord
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
README
after_commit.gemspec
init.rb
test.sqlite3

README

after_commit
============

After_commit enables you to add after_commit methods to ActiveRecord models, which are called once the transaction has been committed to the database.

Typically one needs this in asynchronous processing where you pass on IDs of new records to another thread or post them to a message queue. Using the stock after_XXX
callbacks you cannot be sure that the other thread/process is already seeing the new record (at least if the database is ACID compliant).


Usage:
======

class Blog < ActiveRecord::Base
  include AfterCommit::ActiveRecord
  
  def after_commit_on_create
    # do something
  end

  def after_commit_on_update
    # do something
  end

  def after_commit_on_destroy
    # do something
  end
end


You have to include AfterCommit::ActiveRecord to add the new callbacks. They aren't automatically added to ActiveRecord::Base as tracking the committed
records takes a little overhead and I thought it's better to be able to manually select which models should get that feature.
  
You have to explicitely define the methods like in the example above, something like after_commit_on_create :do_what_ever doesn't work.

Credits:
========
I've taken existing code and made it threadsafe (well hopefully, so it should be safe when running config.threadsafe!). Original sources:

http://github.com/GUI/after_commit/tree/master
http://github.com/freelancing-god/thinking-sphinx