aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-12-06 21:18:15 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-12-06 21:18:15 +0100
commita246a69e92a8ed54eb569a662dbaba16d743b2b7 (patch)
tree3b40780b4ee790367a6dde94d75f2e2608c41e1d /guides/source
parent09dde024d3b85a74d2efc233fc309089757a28cf (diff)
parent5a300b2ed6b603474a7ee44d081da9e69465ec10 (diff)
downloadrails-a246a69e92a8ed54eb569a662dbaba16d743b2b7.tar.gz
rails-a246a69e92a8ed54eb569a662dbaba16d743b2b7.tar.bz2
rails-a246a69e92a8ed54eb569a662dbaba16d743b2b7.zip
Merge pull request #22516 from gsamokovarov/after-create-update-destroy-commit
Introduce after_{create,update,delete}_commit callbacks
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/active_record_callbacks.md19
1 files changed, 19 insertions, 0 deletions
diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md
index 13989a3b33..b5ad3e9411 100644
--- a/guides/source/active_record_callbacks.md
+++ b/guides/source/active_record_callbacks.md
@@ -412,4 +412,23 @@ end
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.
+Since using `after_commit` callback only on create, update or delete is
+common, there are aliases for those operations:
+
+* `after_create_commit`
+* `after_update_commit`
+* `after_destroy_commit`
+
+```ruby
+class PictureFile < ActiveRecord::Base
+ after_destroy_commit :delete_picture_file_from_disk
+
+ def delete_picture_file_from_disk
+ if File.exist?(filepath)
+ File.delete(filepath)
+ end
+ end
+end
+```
+
WARNING. The `after_commit` and `after_rollback` callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.