Skip to content

Commit 8e73e62

Browse files
authored
Merge pull request rails#49885 from p8/guides/transaction-callbacks
Clarify transaction callbacks section [ci-skip]
2 parents 44f914b + 5b6f422 commit 8e73e62

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

guides/source/active_record_callbacks.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ You can declare as many callbacks as you want inside your callback classes.
498498
Transaction Callbacks
499499
---------------------
500500

501-
### Dealing With Consistency
501+
### `after_commit` and `after_rollback`
502502

503503
There are two additional callbacks that are triggered by the completion of a database transaction: [`after_commit`][] and [`after_rollback`][]. These callbacks are very similar to the `after_save` callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your Active Record models need to interact with external systems which are not part of the database transaction.
504504

@@ -527,7 +527,23 @@ end
527527

528528
NOTE: The `:on` option specifies when a callback will be fired. If you don't supply the `:on` option the callback will fire for every action.
529529

530-
### Context Matters
530+
WARNING. When a transaction completes, the `after_commit` or `after_rollback` callbacks are called for all models created, updated, or destroyed within that transaction. However, if an exception is raised within one of these callbacks, the exception will bubble up and any remaining `after_commit` or `after_rollback` methods will _not_ be executed. As such, if your callback code could raise an exception, you'll need to rescue it and handle it within the callback in order to allow other callbacks to run.
531+
532+
WARNING. The code executed within `after_commit` or `after_rollback` callbacks is itself not enclosed within a transaction.
533+
534+
WARNING. In the context of a single transaction, if you interact with multiple
535+
loaded objects that represent the same record in the database, there's a crucial
536+
behavior in the `after_commit` and `after_rollback` callbacks to note. These
537+
callbacks are triggered only for the first object of the specific record that
538+
undergoes a change within the transaction. Other loaded objects, despite
539+
representing the same database record, will not have their respective
540+
`after_commit` or `after_rollback` callbacks triggered. This nuanced behavior is
541+
particularly impactful in scenarios where you expect independent callback
542+
execution for each object associated with the same database record. It can
543+
influence the flow and predictability of callback sequences, leading to potential
544+
inconsistencies in application logic following the transaction.
545+
546+
### Aliases for `after_commit`
531547

532548
Since using the `after_commit` callback only on create, update, or delete is
533549
common, there are aliases for those operations:
@@ -548,10 +564,6 @@ class PictureFile < ApplicationRecord
548564
end
549565
```
550566

551-
WARNING. When a transaction completes, the `after_commit` or `after_rollback` callbacks are called for all models created, updated, or destroyed within that transaction. However, if an exception is raised within one of these callbacks, the exception will bubble up and any remaining `after_commit` or `after_rollback` methods will _not_ be executed. As such, if your callback code could raise an exception, you'll need to rescue it and handle it within the callback in order to allow other callbacks to run.
552-
553-
WARNING. The code executed within `after_commit` or `after_rollback` callbacks is itself not enclosed within a transaction.
554-
555567
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.
556568

557569
```ruby
@@ -573,18 +585,6 @@ irb> @user.save # updating @user
573585
User was saved to database
574586
```
575587

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

590590
There is also [`after_save_commit`][], which is an alias for using the `after_commit` callback for both create and update together:

0 commit comments

Comments
 (0)