aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorप्रथमेश Sonpatki <csonpatki@gmail.com>2017-07-25 12:31:04 +0530
committerGitHub <noreply@github.com>2017-07-25 12:31:04 +0530
commit1d3b6d23109157b794baca09a9d1167fa40a58fe (patch)
treea3a44e8176aa3b20e1b7f184c4dd9c8b06fd86d6
parent65f861ee1e8cdeec1533c565f949d05cf8195965 (diff)
parentc8c16bf87f2a757ce52e13e60fd61a8597e1eadc (diff)
downloadrails-1d3b6d23109157b794baca09a9d1167fa40a58fe.tar.gz
rails-1d3b6d23109157b794baca09a9d1167fa40a58fe.tar.bz2
rails-1d3b6d23109157b794baca09a9d1167fa40a58fe.zip
Merge pull request #29930 from JagdeepSingh/add-callbacks-documentation
[ci skip] Add documentation for after_create_commit and after_update_commit callbacks
-rw-r--r--guides/source/active_record_callbacks.md29
1 files changed, 29 insertions, 0 deletions
diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md
index b1705855d0..fc4f773e3c 100644
--- a/guides/source/active_record_callbacks.md
+++ b/guides/source/active_record_callbacks.md
@@ -428,3 +428,32 @@ end
```
WARNING. The `after_commit` and `after_rollback` callbacks are called for all models created, updated, or destroyed within a transaction block. 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.
+
+WARNING. Using `after_create_commit` and `after_update_commit` both in the same model will override the callback which was registered first amongst them.
+
+```ruby
+class User < ApplicationRecord
+ after_create_commit :log_user_saved_to_db
+ after_update_commit :log_user_saved_to_db
+
+ private
+ def log_user_saved_to_db
+ puts 'User was saved to database'
+ end
+end
+
+# prints nothing
+>> @user = User.create
+
+# updating @user
+>> @user.save
+=> User was saved to database
+```
+
+To register callbacks for both create and update actions, use `after_commit` instead.
+
+```ruby
+class User < ApplicationRecord
+ after_commit :log_user_saved_to_db, on: [:create, :update]
+end
+```