views:

341

answers:

1

I have a complex action inside controller that performs several update queries to the database.

How can I make this action acts like transaction without any structural refactoring?

+1  A: 
MyModel.transaction do
  begin
    @model.update_stuff
    @sub_model.update_stuff
    @sub_sub_model.update_stuff
  rescue ActiveRecord::StatementInvalid # or whatever 
    # rollback is automatic, but if you want to do something additional, 
    # add it here
  end
end

Here are the docs for the transaction method.

Sarah Mei