Skip to content

Commit 79fa0e3

Browse files
Add warning about deduplication of after_xxx_commit shortcuts
1 parent 23af6f3 commit 79fa0e3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

activerecord/lib/active_record/transactions.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ module Transactions
188188
# #after_commit is a good spot to put in a hook to clearing a cache since clearing it from
189189
# within a transaction could trigger the cache to be regenerated before the database is updated.
190190
#
191+
# *Warning*: Callbacks are deduplicated according to the callback and method.
192+
# This means you cannot have multiple <tt>after_xxx_commit</tt> shortcuts calling the same method.
193+
#
194+
# after_create_commit :do_foo #This will NOT fire
195+
# after_save_commit :do_foo
196+
#
197+
# Instead, use after_commit directly
198+
#
199+
# after_commit :do_foo, on: [:create, :save]
200+
#
191201
# === Caveats
192202
#
193203
# If you're on MySQL, then do not use Data Definition Language (DDL) operations in nested
@@ -237,24 +247,32 @@ def after_commit(*args, &block)
237247
end
238248

239249
# Shortcut for <tt>after_commit :hook, on: [ :create, :update ]</tt>.
250+
#
251+
# *Warning*: only one <tt>after_xxx_commit</tt> shortcut can call any given method
240252
def after_save_commit(*args, &block)
241253
set_options_for_callbacks!(args, on: [ :create, :update ], **prepend_option)
242254
set_callback(:commit, :after, *args, &block)
243255
end
244256

245257
# Shortcut for <tt>after_commit :hook, on: :create</tt>.
258+
#
259+
# *Warning*: only one <tt>after_xxx_commit</tt> shortcut can call any given method
246260
def after_create_commit(*args, &block)
247261
set_options_for_callbacks!(args, on: :create, **prepend_option)
248262
set_callback(:commit, :after, *args, &block)
249263
end
250264

251265
# Shortcut for <tt>after_commit :hook, on: :update</tt>.
266+
#
267+
# *Warning*: only one <tt>after_xxx_commit</tt> shortcut can call any given method
252268
def after_update_commit(*args, &block)
253269
set_options_for_callbacks!(args, on: :update, **prepend_option)
254270
set_callback(:commit, :after, *args, &block)
255271
end
256272

257273
# Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
274+
#
275+
# *Warning*: only one <tt>after_xxx_commit</tt> shortcut can call any given method
258276
def after_destroy_commit(*args, &block)
259277
set_options_for_callbacks!(args, on: :destroy, **prepend_option)
260278
set_callback(:commit, :after, *args, &block)

0 commit comments

Comments
 (0)