Module: Mongoid::Criteria::Includable
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criteria/includable.rb
Overview
Module providing functionality for parsing (nested) inclusion definitions.
Instance Method Summary collapse
-
#eager_load(*relations) ⇒ Criteria
Eager loads all the provided associations using aggregation $lookup.
-
#includes(*relations) ⇒ Criteria
Eager loads all the provided associations.
-
#inclusions ⇒ Array<Mongoid::Association::Relatable>
Get a list of criteria that are to be executed for eager loading.
-
#inclusions=(value) ⇒ Array<Mongoid::Association::Relatable>
Set the inclusions for the criteria.
-
#use_lookup? ⇒ true | false
Returns whether to use $lookup aggregation for eager loading.
Instance Method Details
#eager_load(*relations) ⇒ Criteria
Eager loads all the provided associations using aggregation $lookup. The behavior should be identical to #includes.
41 42 43 44 45 |
# File 'lib/mongoid/criteria/includable.rb', line 41 def eager_load(*relations) extract_includes_list(klass, nil, true, *relations) @use_lookup = ! clone end |
#includes(*relations) ⇒ Criteria
This will work for embedded associations that reference another collection via belongs_to as well.
Eager loading brings all the documents into memory, so there is a sweet spot on the performance gains. Internal benchmarks show that eager loading becomes slower around 100k documents, but this will naturally depend on the specific application.
Eager loads all the provided associations. Will load all the documents into the identity map whose ids match based on the extra query for the ids.
26 27 28 29 |
# File 'lib/mongoid/criteria/includable.rb', line 26 def includes(*relations) extract_includes_list(klass, nil, false, *relations) clone end |
#inclusions ⇒ Array<Mongoid::Association::Relatable>
Get a list of criteria that are to be executed for eager loading.
59 60 61 |
# File 'lib/mongoid/criteria/includable.rb', line 59 def inclusions @inclusions ||= [] end |
#inclusions=(value) ⇒ Array<Mongoid::Association::Relatable>
Set the inclusions for the criteria.
68 69 70 |
# File 'lib/mongoid/criteria/includable.rb', line 68 def inclusions=(value) @inclusions = value end |
#use_lookup? ⇒ true | false
Returns whether to use $lookup aggregation for eager loading. Only when eager_load was requested and there is something to load: an empty inclusion list (e.g. eager_load([])) falls back to the normal path.
52 53 54 |
# File 'lib/mongoid/criteria/includable.rb', line 52 def use_lookup? !!@use_lookup && inclusions.any? end |