@@ -12,28 +12,28 @@ module ActiveRecord
12
12
# After updating the database state, you may sometimes need to perform some extra work, or reflect these
13
13
# changes in a remote system like clearing or updating a cache:
14
14
#
15
- # def publish_article(article)
16
- # article.update!(published: true)
17
- # NotificationService.article_published(article)
18
- # end
15
+ # def publish_article(article)
16
+ # article.update!(published: true)
17
+ # NotificationService.article_published(article)
18
+ # end
19
19
#
20
20
# The above code works but has one important flaw, which is that it no longer works properly if called inside
21
21
# a transaction, as it will interact with the remote system before the changes are persisted:
22
22
#
23
- # Article.transaction do
24
- # article = create_article(article)
25
- # publish_article(article)
26
- # end
23
+ # Article.transaction do
24
+ # article = create_article(article)
25
+ # publish_article(article)
26
+ # end
27
27
#
28
28
# The callbacks offered by ActiveRecord::Transaction allow to rewriting this method in a way that is compatible
29
29
# with transactions:
30
30
#
31
- # def publish_article(article)
32
- # article.update!(published: true)
33
- # Article.current_transaction.after_commit do
34
- # NotificationService.article_published(article)
31
+ # def publish_article(article)
32
+ # article.update!(published: true)
33
+ # Article.current_transaction.after_commit do
34
+ # NotificationService.article_published(article)
35
+ # end
35
36
# end
36
- # end
37
37
#
38
38
# In the above example, if +publish_article+ is called inside a transaction, the callback will be invoked
39
39
# after the transaction is successfully committed, and if called outside a transaction, the callback will be invoked
0 commit comments