Ruby

Ruby

Made by DeepSource

ActiveRecord callback being overridden RB-LI1106

Anti-pattern
Major

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

References

  1. The unexpected after_commit behaviour