Module: Mongoid::Criteria::Findable
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criteria/findable.rb
Instance Method Summary collapse
-
#execute_or_raise(ids, multi) ⇒ Document+
Execute the criteria or raise an error if no documents found.
-
#find(*args) ⇒ Array<Document>, Document
Find the matchind document(s) in the criteria for the provided ids.
-
#for_ids(ids) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies an id that must be matched. -
#multiple_from_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
Instance Method Details
#execute_or_raise(ids, multi) ⇒ Document+
Execute the criteria or raise an error if no documents found.
18 19 20 21 22 |
# File 'lib/mongoid/criteria/findable.rb', line 18 def execute_or_raise(ids, multi) result = multiple_from_db(ids) check_for_missing_documents!(result, ids) multi ? result : result.first end |
#find(*args) ⇒ Array<Document>, Document
Find the matchind document(s) in the criteria for the provided ids.
35 36 37 38 39 |
# File 'lib/mongoid/criteria/findable.rb', line 35 def find(*args) ids = args.__find_args__ raise_invalid if ids.any?(&:nil?) for_ids(ids).execute_or_raise(ids, args.multi_arged?) end |
#for_ids(ids) ⇒ Criteria
Adds a criterion to the Criteria
that specifies an id that must be matched.
52 53 54 55 56 57 58 59 |
# File 'lib/mongoid/criteria/findable.rb', line 52 def for_ids(ids) ids = mongoize_ids(ids) if ids.size > 1 send(id_finder, { _id: { "$in" => ids }}) else send(id_finder, { _id: ids.first }) end end |
#multiple_from_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
70 71 72 73 74 |
# File 'lib/mongoid/criteria/findable.rb', line 70 def multiple_from_db(ids) return entries if ids = mongoize_ids(ids) ids.empty? ? [] : from_database(ids) end |