Class: Mongoid::Association::Eager

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/association/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:



19
20
21
22
23
# File 'lib/mongoid/association/eager.rb', line 19

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.



31
32
33
34
35
36
37
38
# File 'lib/mongoid/association/eager.rb', line 31

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