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.

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



20
21
22
23
24
# File 'build/mongoid-master/lib/mongoid/association/referenced/eager.rb', line 20

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.



32
33
34
35
36
37
38
39
# File 'build/mongoid-master/lib/mongoid/association/referenced/eager.rb', line 32

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