Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Callbacks Documentation: Specify 'after_commit' and 'after_rollback' Behavior for Multiple Objects of the Same Record [ci skip] #49831

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions guides/source/active_record_callbacks.md
Expand Up @@ -554,6 +554,18 @@ WARNING. The code executed within `after_commit` or `after_rollback` callbacks i

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.

WARNING. In the context of a single transaction, if you interact with multiple
loaded objects that represent the same record in the database, there's a crucial
behavior in the `after_commit` and `after_rollback` callbacks to note. These
callbacks are triggered only for the first object of the specific record that
undergoes a change within the transaction. Other loaded objects, despite
representing the same database record, will not have their respective
`after_commit` or `after_rollback` callbacks triggered. This nuanced behavior is
particularly impactful in scenarios where you expect independent callback
execution for each object associated with the same database record. It can
influence the flow and predictability of callback sequences, leading to potential
inconsistencies in application logic following the transaction.

```ruby
class User < ApplicationRecord
after_create_commit :log_user_saved_to_db
Expand Down