There can only be one
after_*_commit :broadcast
hook defined for a model.10
11class Comment < ApplicationRecord
12 after_create_commit :broadcast
13 after_destroy_commit :broadcast14
15 def broadcast
16 BroadcastJob.perform_later(id)
Description
There should only be one call to after_commit
(and its aliases - after_create_commit
, after_update_commit
, and after_destroy_commit
) with the same callback name per model.
Bad practice
# This won't be triggered.
after_create_commit :log_action
# This will override the callback added by
# after_create_commit.
after_update_commit :log_action
# This won't be triggered.
after_commit :log_action, on: :create
# This won't be triggered.
after_update_commit :log_action
# This will override both previous callbacks.
after_commit :log_action, on: :destroy
Recommended
after_save_commit :log_action
after_create_commit :log_create_action
after_update_commit :log_update_action