Mongoid.master.collection("seq").find_and_modify({
:query => {:_id => self.class.name},
:update => {'$inc' => {:next => 1}},
:new => true,
:upsert => true
})["next"]它在mongoid 2.4.9中工作得很好,但是当我使用mongoid 3.0.0时,我得到了错误
NoMethodError in PostsController#new
undefined method `master' for Mongoid:ModuleMongoid3.0.0不支持Mongoid.master?
发布于 2012-05-29 13:14:14
因为Mongoid 3.0.0是用Moped代替Mongo Ruby驱动程序,所以旧的API不能调用not。
您可以尝试这样做:
Mongoid::Sessions.default.command({:findAndModify => "seq",
:query => { :_id => self.class.name },
:update => { "$inc" => { :next => 1 } },
:upsert => true,
:new => true })你可以使用这个Gem来实现自动递增id特性:https://github.com/huacnlee/mongoid_auto_increment_id
https://stackoverflow.com/questions/10469521
复制相似问题