You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/active_record_callbacks.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -498,7 +498,7 @@ You can declare as many callbacks as you want inside your callback classes.
498
498
Transaction Callbacks
499
499
---------------------
500
500
501
-
### Dealing With Consistency
501
+
### `after_commit` and `after_rollback`
502
502
503
503
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.
504
504
@@ -527,7 +527,23 @@ end
527
527
528
528
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.
529
529
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`
531
547
532
548
Since using the `after_commit` callback only on create, update, or delete is
533
549
common, there are aliases for those operations:
@@ -548,10 +564,6 @@ class PictureFile < ApplicationRecord
548
564
end
549
565
```
550
566
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
-
555
567
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.
0 commit comments