Has anyone implemented an after_commit
hook in Rails ? I am not looking for model based after commits on update/create/etc, I want to be able to dynamically define a block that will be executed only if the current (top-most) transaction passes:
def remove_file
current_transaction.after_commit do
FileUtils.rm(file_path)
end
end
Any idea if this has already been implemented, if it's going to be in rails 3.0 ?
When a transaction completes, the after_commit or after_rollback callbacks are called for all models created, updated, or destroyed within that transaction.
after_commit is a type of active record callback.
Abstract Controller Callbacks Abstract Controller provides hooks during the life cycle of a controller action. Callbacks allow you to trigger logic during this cycle. Available callbacks are: after_action. append_after_action.
Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.
Rails 3 provides after_commit/after_rollback callbacks: https://github.com/rails/rails/commit/da840d13da865331297d5287391231b1ed39721b
In Rails 1 and 2 you can get the same functionality by using this gem: https://github.com/freelancing-god/after_commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With