2
 Mongoid.master.collection("seq").find_and_modify({
   :query  => {:_id   => self.class.name},
   :update => {'$inc' => {:next => 1}},
   :new    => true,
   :upsert => true
})["next"]

It works well in mongoid 2.4.9, but when i use mongoid 3.0.0, i got error

NoMethodError in PostsController#new

undefined method `master' for Mongoid:Module

Mongoid 3.0.0 does not support Mongoid.master ?

doabit
  • 68
  • 6

1 Answers1

3

Because Mongoid 3.0.0 was use Moped to instead Mongo Ruby Driver, So the old API can't call not.

You can try this:

Mongoid::Sessions.default.command({:findAndModify => "seq",
                                         :query  => { :_id => self.class.name },
                                         :update => { "$inc" => { :next => 1 } },
                                         :upsert => true,
                                         :new    => true })

And you can use this Gem to do Auto increment id feature: https://github.com/huacnlee/mongoid_auto_increment_id

huacnlee
  • 430
  • 3
  • 9