Module: Mongoid::Association::EagerLoadable

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

Overview

This module defines the eager loading behavior for criteria.

Instance Method Summary collapse

Instance Method Details

#eager_load(docs) ⇒ Object



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

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

#eager_loadable?Boolean

Returns:



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

def eager_loadable?
  !criteria.inclusions.empty?
end

#preload(relations, docs) ⇒ Object



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

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