使用Mongoid在Rails中执行原子块事务

Lui*_*spo 1 transactions ruby-on-rails mongoid

我在我的应用程序中使用Rails 4.1.4Mongoid.

我只是想知道是否有办法在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
Run Code Online (Sandbox Code Playgroud)

我想将该代码包装在单个原子事务中,我的意思是,如果出现问题,我不希望将两个实例(some_model_instance,other_model_instance)都保存到数据库中.

我已经看到ActiveRecord有点像这样:

SomeModel.transaction do
    # do stuff I did above
end
Run Code Online (Sandbox Code Playgroud)

这对Mongoid有用吗?

谢谢你的帮助!!!.

Lui*_*spo 6

封闭的问题

MongoDB不支持事务.它仅对单个文档执行原子操作.所以没有办法与Mongoid进行"原子块事务".

最好.