instance method after_commit

Ruby on Rails 3.2.22.5

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

after_commit(*args, &block)

This callback is called after a record has been created, updated, or destroyed.

You can specify that the callback should only be fired by a certain action with the :on option:

after_commit :do_foo, :on => :create
after_commit :do_bar, :on => :update
after_commit :do_baz, :on => :destroy

Also, to have the callback fired on create and update, but not on destroy:

after_commit :do_zoo, :if => :persisted?

Note that transactional fixtures do not play well with this feature. Please use the test_after_commit gem to have these hooks fired in tests.

Parameters

args rest
block block
Source
# File activerecord/lib/active_record/transactions.rb, line 226
      def after_commit(*args, &block)
        options = args.last
        if options.is_a?(Hash) && options[:on]
          options[:if] = Array.wrap(options[:if])
          options[:if] << "transaction_include_action?(:#{options[:on]})"
        end
        set_callback(:commit, :after, *args, &block)
      end

Defined in activerecord/lib/active_record/transactions.rb line 226 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Transactions::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close