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

Inherits:
Object
  • Object
show all
Defined in:
build/mongoid-master/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 associations

Since:

  • 4.0.0



25
26
27
28
29
# File 'build/mongoid-master/lib/mongoid/association/referenced/eager.rb', line 25

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

Instance Method Details

#runArray

Run the preloader.

Examples:

Preload the associations into the documents.

loader.run

Returns:

  • (Array)

    The list of documents given.

Since:

  • 4.0.0



39
40
41
42
43
44
45
46
# File 'build/mongoid-master/lib/mongoid/association/referenced/eager.rb', line 39

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