def after_transaction(opts = {}, &block)

      # default options
      default_options = { only: nil, if_no_transaction: :run}
      opts = default_options.merge(opts)

      # normalize opts to string keys
      normalized = opts.dup
      opts.each{ |k,v| normalized[k.to_s] = v }
      opts = normalized


      if ActiveRecord::Base.connection.open_transactions > 0
        callbacks = ActiveRecord::Base.callbacks

        case opts['only']
        when :commit
          callbacks.store(:commit,&block)
        when :rollback
          callbacks.store(:rollback,&block)
        else
          # both cases
          callbacks.store(:commit,&block)
          callbacks.store(:rollback,&block)
        end
      else
        if opts['if_no_transaction'] == :run
          block.call
        end
      end
    end
def after_transaction(opts = {}, &block)

      # default options
      default_options = { only: nil, if_no_transaction: :run}
      opts = default_options.merge(opts)

      # normalize opts to string keys
      normalized = opts.dup
      opts.each{ |k,v| normalized[k.to_s] = v }
      opts = normalized


      if ActiveRecord::Base.connection.open_transactions > 0
        callbacks = ActiveRecord::Base.callbacks

        case opts['only']
        when :commit
          callbacks.store(:commit,&block)
        when :rollback
          callbacks.store(:rollback,&block)
        else
          # both cases
          callbacks.store(:commit,&block)
          callbacks.store(:rollback,&block)
        end
      else
        if opts['if_no_transaction'] == :run
          block.call
        end
      end
    end
Allows to execute any block of code after transaction completes.
 If no transaction is actually open, the code runs immediately.

 Accepts the following options that modifies this behavior:

 * <tt>:only</tt> - Execute this code only on commit or only on
   rollback. Accepts one of the following symbols: <tt>:commit</tt>,
   <tt>:rollback</tt>.
 * <tt>:if_no_transaction</tt> - Specifies what to do if there is no
   active transaction. Accepts one of the following symbols:
   <tt>:run</tt> (default), <tt>:skip</tt> (do not run).

 Example of use:

   ActiveRecord::Base.transaction do
     # executes some code
     puts "runs within transaction"
     after_transaction do
       # things to do after transaction
       puts "runs after transaction"
     end
     # executes more code
     puts "again runs within transaction"
   end

 will output

   runs within transaction
   again runs within transaction
   runs after transaction