callback executed like after_save but after transaction

I'm trying to add some code for my ActiveRecord class so that it is
executed whenever an object is updated, this code is a seperate
process that reads from the same table represented by my model class
so when it runs it needs the database to be up to date.

In the following situation this is fine:

# MyModel
def after_save
  `/usr/local/bin/update_models -i #{}`

# controller action delete
def delete
  MyModel.find(params[:id]).update_attribute(:deleted_at, #
after_save is called when the table

But in this situation:

# controller action new
def new
  MyModel.transaction do
    newmodel =
    othermodel =
    ... other code ...!!

after_save gets called inside the transaction, and so the database is
not up to date! I've looked into ActiveRecord::Observers but this also
faces the same problem. Is there any way around this?

# Here is my idea for how this might be achieved, given no knowledge
of how to accomplish it the rails way
# comments / feedback / criticism welcomed
# (note this is just a proof of concept not rails compatible code)

class ActiveRecord

  def self.transaction(&block)
    Thread.current['after_commit_procs'] ||= []

    Thread.current['after_commit_procs'].each do |proc|

  def save
    if self.respond_to?(:after_commit) then
      Thread.current['after_commit_procs'] <<
    puts "Saving..."


class MyRecord < ActiveRecord

  def after_commit
    puts "after_commit callback called!"

MyRecord.transaction do
  rec =
  puts "Before save"
  puts "After save, before commit"
puts "After commit"

# output:
# Before save
# Saving...
# After save, before commit
# after_commit callback called!
# After commit

Thinking Sphinx defines an after_commit callback to run the indexer
for delta indexes. It is based on this post:

You can check the actual code in lib/thinking_sphinx/active_record/