Method: MongoModel::Scope::FinderMethods#find

Defined in:
lib/mongomodel/support/scope/finder_methods.rb

#find(*ids, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 4

def find(*ids, &block)
  if block_given?
    to_a.find(&block)
  else
    ids.flatten!

    case ids.size
    when 0
      raise ArgumentError, "At least one id must be specified"
    when 1
      id = ids.first
      where(:id => id).first || raise(DocumentNotFound, "Couldn't find document with id: #{id}")
    else
      ids = ids.map { |id| Reference.cast(id) }
      docs = where(:id.in => ids).to_a
      raise DocumentNotFound if docs.size != ids.size
      docs.sort_by { |doc| ids.index(doc.id) }
    end
  end
end