Module: Mongoid::Association::EagerLoadable

Included in:
Contextual::Memory, Contextual::Mongo
Defined in:
build/mongoid-7.0/lib/mongoid/association/eager_loadable.rb

Overview

This module defines the eager loading behavior for criteria.

Since:

  • 7.0

Instance Method Summary collapse

Instance Method Details

#eager_load(docs) ⇒ Object

Since:

  • 7.0



16
17
18
19
20
21
22
# File 'build/mongoid-7.0/lib/mongoid/association/eager_loadable.rb', line 16

def eager_load(docs)
  docs.tap do |d|
    if eager_loadable?
      preload(criteria.inclusions, d)
    end
  end
end

#eager_loadable?Boolean

Returns:

Since:

  • 7.0



12
13
14
# File 'build/mongoid-7.0/lib/mongoid/association/eager_loadable.rb', line 12

def eager_loadable?
  !criteria.inclusions.empty?
end

#preload(relations, docs) ⇒ Object

Since:

  • 7.0



24
25
26
27
28
29
30
31
32
33
# File 'build/mongoid-7.0/lib/mongoid/association/eager_loadable.rb', line 24

def preload(relations, docs)
  relations.group_by(&:inverse_class_name)
      .values
      .each do |associations|
    associations.group_by(&:relation)
        .each do |relation, association|
      relation.eager_loader(association, docs).run
    end
  end
end