Ruby on rails 使用RubyonRails的事务操作
我在控制器中有一个复杂的操作,它对数据库执行几个更新查询 在没有任何结构重构的情况下,如何使此操作像事务一样运行Ruby on rails 使用RubyonRails的事务操作,ruby-on-rails,transactions,actioncontroller,Ruby On Rails,Transactions,Actioncontroller,我在控制器中有一个复杂的操作,它对数据库执行几个更新查询 在没有任何结构重构的情况下,如何使此操作像事务一样运行 MyModel.transaction do begin @model.update_stuff @sub_model.update_stuff @sub_sub_model.update_stuff rescue ActiveRecord::StatementInvalid # or whatever # rollback is autom
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
以下是。可以通过以下方式使控制器中的所有操作同时具有事务性:
around_filter :transactional
def transactional
ActiveRecord::Base.transaction do
yield
end
end
面向方面编程。有什么Ruby不能做的吗?(除了在Windows上工作之外。)