2009-06-01 4 views

Antwort

6
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 

Hier sind the docs for the transaction method.

4

Es ist posible alle Aktionen im Controller Transaktions zu machen sofort mit:

around_filter :transactional 

def transactional 
    ActiveRecord::Base.transaction do 
    yield 
    end 
end 
+0

Aspektorientierte Programmierung. Gibt es nichts, was Ruby nicht kann? (Neben der Arbeit an Windows.) – Chloe