Module: MongoMapper::Plugins::AutoIncrementId

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongo_mapper/plugins/auto_increment_id.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_document_id(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongo_mapper/plugins/auto_increment_id.rb', line 14

def generate_document_id(options={})
  while true
    oldest_number = self.class.fields(:_id).sort([[:_id, :descending]]).first.try(:id)
    self.id = oldest_number.to_i + 1
    begin
      break if collection.insert({:_id => id}, :safe => true)
    rescue Mongo::OperationFailure => e
      #Ignored, trying to get the next key
    end
  end
end