Module: Mongoid::Association::EagerLoadable

Included in:
Contextual::Memory, Contextual::Mongo
Defined in:
build/mongoid-7.3/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



18
19
20
21
22
23
24
# File 'build/mongoid-7.3/lib/mongoid/association/eager_loadable.rb', line 18

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



14
15
16
# File 'build/mongoid-7.3/lib/mongoid/association/eager_loadable.rb', line 14

def eager_loadable?
  !criteria.inclusions.empty?
end

#preload(relations, docs) ⇒ Object

Since:

  • 7.0



26
27
28
29
30
31
32
33
34
35
# File 'build/mongoid-7.3/lib/mongoid/association/eager_loadable.rb', line 26

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