Skip to content

calling methods that trigger callbacks can cause after_commit callbacks to run multiple times #29

@DeedleFake

Description

@DeedleFake

If an after_commit is called inside of a transaction and that transaction has calls to model methods that can trigger model callbacks, the block passed to after_commit will be called multiple times. For example,

ActiveRecord::Base.transaction do
  User.create!(email:)
  ev = Event.create!(name: 'created user', source: 'admin')

  after_commit do
    send_welcome_email!
  end

  track_event!(ev)
end

When this transaction runs, the welcome e-mail will be sent multiple times because the block is run repeatedly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions