aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-02-24 20:43:09 +0100
committerYves Senn <yves.senn@gmail.com>2013-02-24 20:43:09 +0100
commit74bc0d0fd4161a80af4337797f0e65535114acab (patch)
tree836341ad30f6b79d8182eeef98ef111603125a67 /guides
parentd65376fce4ea806e489d1fb985bc9393bcd2e0e2 (diff)
downloadrails-74bc0d0fd4161a80af4337797f0e65535114acab.tar.gz
rails-74bc0d0fd4161a80af4337797f0e65535114acab.tar.bz2
rails-74bc0d0fd4161a80af4337797f0e65535114acab.zip
update the example for `after_commit` in the guides.
This is a follow up to #9356.
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_callbacks.md16
1 files changed, 7 insertions, 9 deletions
diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md
index 516457bcd3..bb42fab101 100644
--- a/guides/source/active_record_callbacks.md
+++ b/guides/source/active_record_callbacks.md
@@ -342,19 +342,17 @@ By using the `after_commit` callback we can account for this case.
```ruby
class PictureFile < ActiveRecord::Base
- attr_accessor :delete_file
+ after_commit :delete_picture_file_from_disk, :on => [:destroy]
- after_destroy do |picture_file|
- picture_file.delete_file = picture_file.filepath
- end
-
- after_commit do |picture_file|
- if picture_file.delete_file && File.exist?(picture_file.delete_file)
- File.delete(picture_file.delete_file)
- picture_file.delete_file = nil
+ def delete_picture_file_from_disk
+ if File.exist?(filepath)
+ File.delete(filepath)
end
end
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.
+
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.