123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Referenced::HasMany::Eager

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Association::Eager
Defined in: lib/mongoid/association/referenced/has_many/eager.rb

Overview

Eager class for has_many associations.

Class Method Summary

::Mongoid::Association::Eager - Inherited

.new

Instantiate the eager load class.

Instance Method Summary

::Mongoid::Association::Eager - Inherited

#run

Run the preloader.

#each_loaded_document_of_class

Retrieves the documents of the specified class, that have the foreign key included in the specified list of keys.

#shift_association

Shift the current association metadata.

Constructor Details

This class inherits a constructor from Mongoid::Association::Eager

Instance Method Details

#group_by_key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_many/eager.rb', line 34

def group_by_key
  @association.primary_key
end

#key (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_many/eager.rb', line 38

def key
  @association.foreign_key
end

#preload (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_many/eager.rb', line 14

def preload
  @docs.each do |d|
    set_relation(d, [])
  end

  entries = Hash.new { |hash, key| hash[key] = [] }
  each_loaded_document do |doc|
    fk = doc.send(key)
    entries[fk] << doc
  end

  entries.each do |id, docs|
    set_on_parent(id, docs)
  end
end

#set_relation(doc, element) (private)

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_many/eager.rb', line 30

def set_relation(doc, element)
  doc.__build__(@association.name, element, @association) unless doc.blank?
end