Class: Mongoid::Association::Referenced::Eager::Base

Inherits:
Object
  • Object
show all
Defined in:
build/mongoid-7.0/lib/mongoid/association/referenced/eager.rb

Overview

Base class for eager load preload functions.

Since:

  • 4.0.0

Instance Method Summary collapse

Constructor Details

#initialize(associations, docs) ⇒ Base

Instantiate the eager load class.

Examples:

Create the new belongs to eager load preloader.

BelongsTo.new(association, parent_docs)

Parameters:

  • associations (Array<Association>)

    Associations to eager load

  • docs (Array<Document>)

    Documents to preload the relations

Since:

  • 4.0.0



23
24
25
26
27
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/eager.rb', line 23

def initialize(associations, docs)
  @associations = associations
  @docs = docs
  @grouped_docs = {}
end

Instance Method Details

#runArray

Run the preloader.

Examples:

Preload the relations into the documents.

loader.run

Returns:

  • (Array)

    The list of documents given.

Since:

  • 4.0.0



37
38
39
40
41
42
43
44
# File 'build/mongoid-7.0/lib/mongoid/association/referenced/eager.rb', line 37

def run
  @loaded = []
  while shift_association
    preload
    @loaded << @docs.collect { |d| d.send(@association.name) if d.respond_to?(@association.name) }
  end
  @loaded.flatten
end