diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-06 19:15:33 +0200 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2015-12-06 20:33:36 +0200 |
commit | 5a300b2ed6b603474a7ee44d081da9e69465ec10 (patch) | |
tree | 1b9374dba7a2f9bdfcb8987fcc3f7d7fa2e53830 /guides | |
parent | 65443ceb0d55e1445b28e31e772629424e6755c3 (diff) | |
download | rails-5a300b2ed6b603474a7ee44d081da9e69465ec10.tar.gz rails-5a300b2ed6b603474a7ee44d081da9e69465ec10.tar.bz2 rails-5a300b2ed6b603474a7ee44d081da9e69465ec10.zip |
Introduce after_{create,update,delete}_commit callbacks
Those are actually shortcuts for `after_commit`.
Before:
after_commit :add_to_index_later, on: :create
after_commit :update_in_index_later, on: :update
after_commit :remove_from_index_later, on: :destroy
After:
after_create_commit :add_to_index_later
after_update_commit :update_in_index_later
after_destroy_commit :remove_from_index_later
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_callbacks.md | 19 |
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. |