【发布时间】:2014-10-03 16:24:26
【问题描述】:
我在我的应用中使用 Rails 4.1.4 和 Mongoid。
我只是想知道是否有一种方法可以在 Rails 中使用 Mongoid 原子地执行块事务。
为了清楚起见,假设我有这个:
some_model_instance = SomeModel.find(some_id)
some_model_instance.some_attribute = "Some Attribute Value"
some_model_instance.save
other_model_instance = OtherModel.find(other_id)
other_model_instance.other_attribute = "Other Attribute Value"
other_model_instance.save
我想将该代码包装在单个原子事务中,我的意思是,如果出现问题,我不希望两个实例(some_model_instance、other_model_instance)都保存到数据库中。
我已经看到 ActiveRecord 是这样的:
SomeModel.transaction do
# do stuff I did above
end
这也适用于 Mongoid?。
感谢您的帮助!!!。
【问题讨论】:
-
MongoDB 没有事务,所以你必须像穴居人一样自己做。
标签: ruby-on-rails transactions mongoid