mongodb/mongoid · error · ArgumentError

:on conditions for after_commit and after_rollback…

Error message

:on conditions for after_commit and after_rollback callbacks have to be one of #{CALLBACK_ACTIONS}

What it means

assert_valid_transaction_action validates the :on option of after_commit/after_rollback callbacks: each listed action must be one of the known transaction callback actions (:create, :update, :destroy, :create_or_update); anything else is rejected when the callback is declared.

Solutions

  1. Use only the documented :on actions for transaction callbacks
  2. Remove the :on option to fire the callback for every action
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/clients/sessions.rb:226 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/9a497d3ef06a0e10. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/clients/sessions.rb:226

          return unless options[:on]

          fire_on = Array(options[:on])
          assert_valid_transaction_action(fire_on)
          options[:if] = [
            -> { transaction_include_any_action?(fire_on) },
            *options[:if]
          ]
        end

        # Asserts that the given actions are valid for after_commit
        # and after_rollback callbacks.
        #
        # @param [ Array<Symbol> ] actions Actions to be checked.
        # @raise [ ArgumentError ] If any of the actions is not valid.
        def assert_valid_transaction_action(actions)
          return unless (actions - CALLBACK_ACTIONS).any?

          raise ArgumentError,
                ":on conditions for after_commit and after_rollback callbacks have to be one of #{CALLBACK_ACTIONS}"
        end

        def transaction_include_any_action?(actions)
          actions.any? do |action|
            case action
            when :create
              persisted? && previously_new_record?
            when :update
              !(previously_new_record? || destroyed?)
            when :destroy
              destroyed?
            end
          end
        end
      end

      private

View on GitHub (pinned to 0da0e23d71)