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

Instance Method Details

#eager_load(*relations) ⇒ Criteria

Eager loads all the provided associations using aggregation $lookup. The behavior should be identical to #includes.

Examples:

Eager load the provided associations.

Person.eager_load(:posts, :game)

Parameters:

  • *relations ([ Symbol | Hash ]...)

    The names of the association(s) to eager load.

Returns:



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 = !embedded?
  clone
end

#includes(*relations) ⇒ Criteria

Note:

This will work for embedded associations that reference another collection via belongs_to as well.

Note:

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.

Examples:

Eager load the provided associations.

Person.includes(:posts, :game)

Parameters:

  • *relations ([ Symbol | Hash ]...)

    The names of the association(s) to eager load.

Returns:



26
27
28
29
# File 'lib/mongoid/criteria/includable.rb', line 26

def includes(*relations)
  extract_includes_list(klass, nil, false, *relations)
  clone
end

#inclusionsArray<Mongoid::Association::Relatable>

Get a list of criteria that are to be executed for eager loading.

Returns:



57
58
59
# File 'lib/mongoid/criteria/includable.rb', line 57

def inclusions
  @inclusions ||= []
end

#inclusions=(value) ⇒ Array<Mongoid::Association::Relatable>

Set the inclusions for the criteria.

Parameters:

Returns:



66
67
68
# File 'lib/mongoid/criteria/includable.rb', line 66

def inclusions=(value)
  @inclusions = value
end

#use_lookup?true | false

Returns whether to use $lookup aggregation for eager loading.

Returns:

  • (true | false)

    Whether to use $lookup.



50
51
52
# File 'lib/mongoid/criteria/includable.rb', line 50

def use_lookup?
  !!@use_lookup
end