Module: Mongoid::Criteria::Findable
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criteria/findable.rb
Overview
Mixin module included in Mongoid::Criteria which adds the ability to find document by id.
Instance Method Summary collapse
-
#execute_or_raise(ids, multi) ⇒ Document | Array<Document>
Execute the criteria or raise an error if no documents found.
-
#find(*args) ⇒ Document | Array<Document>
Find the matching document(s) in the criteria for the provided id(s).
-
#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 | Array<Document>
Execute the criteria or raise an error if no documents found.
23 24 25 26 27 |
# File 'lib/mongoid/criteria/findable.rb', line 23 def execute_or_raise(ids, multi) result = multiple_from_db(ids) check_for_missing_documents!(result, ids) multi ? result : result.first end |
#find(*args) ⇒ Document | Array<Document>
Each argument can be an individual id, an array of ids or a nested array. Each array will be flattened.
Find the matching document(s) in the criteria for the provided id(s).
43 44 45 46 47 |
# File 'lib/mongoid/criteria/findable.rb', line 43 def find(*args) ids = prepare_ids_for_find(args) raise_invalid if ids.any?(&:nil?) for_ids(ids).execute_or_raise(ids, multi_args?(args)) end |
#for_ids(ids) ⇒ Criteria
Adds a criterion to the Criteria
that specifies an id that must be matched.
60 61 62 63 64 65 66 67 |
# File 'lib/mongoid/criteria/findable.rb', line 60 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.
78 79 80 81 82 |
# File 'lib/mongoid/criteria/findable.rb', line 78 def multiple_from_db(ids) return entries if ids = mongoize_ids(ids) ids.empty? ? [] : from_database(ids) end |