Skip to content

Commit 1370d04

Browse files
Merge pull request rails#49831 from dhavalsingh/update_callback_docs_for_same_model_multi_instance
Update Callbacks Documentation: Specify 'after_commit' and 'after_rollback' Behavior for Multiple Objects of the Same Record [ci skip]
2 parents 994ceea + 2e6f1dc commit 1370d04

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

guides/source/active_record_callbacks.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,18 @@ WARNING. The code executed within `after_commit` or `after_rollback` callbacks i
554554

555555
WARNING. Using both `after_create_commit` and `after_update_commit` with the same method name will only allow the last callback defined to take effect, as they both internally alias to `after_commit` which overrides previously defined callbacks with the same method name.
556556

557+
WARNING. In the context of a single transaction, if you interact with multiple
558+
loaded objects that represent the same record in the database, there's a crucial
559+
behavior in the `after_commit` and `after_rollback` callbacks to note. These
560+
callbacks are triggered only for the first object of the specific record that
561+
undergoes a change within the transaction. Other loaded objects, despite
562+
representing the same database record, will not have their respective
563+
`after_commit` or `after_rollback` callbacks triggered. This nuanced behavior is
564+
particularly impactful in scenarios where you expect independent callback
565+
execution for each object associated with the same database record. It can
566+
influence the flow and predictability of callback sequences, leading to potential
567+
inconsistencies in application logic following the transaction.
568+
557569
```ruby
558570
class User < ApplicationRecord
559571
after_create_commit :log_user_saved_to_db

0 commit comments

Comments
 (0)