Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails after_commit?

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 ?

like image 400
Anna B Avatar asked Feb 08 '10 10:02

Anna B


People also ask

What does After_commit do in Rails?

When a transaction completes, the after_commit or after_rollback callbacks are called for all models created, updated, or destroyed within that transaction.

What is After_commit?

after_commit is a type of active record callback.

What is controller callback in rails?

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.

What are active records in Rails?

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.


1 Answers

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

like image 78
severin Avatar answered Sep 21 '22 06:09

severin