aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-02-24 11:47:27 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-02-24 11:47:27 -0800
commitaff623254c3e1480eccf15e0f671404838aa327a (patch)
treef3cf7e50ac71ecc191f7592e49a57348677e3b5c /guides
parent05857ec30e6e496ce01aa2e428abd0ab5fae2e92 (diff)
parent74bc0d0fd4161a80af4337797f0e65535114acab (diff)
downloadrails-aff623254c3e1480eccf15e0f671404838aa327a.tar.gz
rails-aff623254c3e1480eccf15e0f671404838aa327a.tar.bz2
rails-aff623254c3e1480eccf15e0f671404838aa327a.zip
Merge pull request #9402 from senny/9356_update_guides
update the example for `after_commit` in the guides.
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.