Transaction Action with Ruby On Rails

送分小仙女□ 提交于 2019-12-05 08:24:57
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.

It's posible to make all actions in controller transactional at once with:

around_filter :transactional

def transactional
  ActiveRecord::Base.transaction do
    yield
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!