Class: Mongoid::Relations::Eager::HasMany
- Inherits:
-
Base
- Object
- Base
- Mongoid::Relations::Eager::HasMany
show all
- Defined in:
- lib/mongoid/relations/eager/has_many.rb
Instance Method Summary
collapse
Methods inherited from Base
#each_loaded_document, #grouped_docs, #initialize, #keys_from_docs, #run, #set_on_parent, #shift_metadata
Instance Method Details
#group_by_key ⇒ Object
28
29
30
|
# File 'lib/mongoid/relations/eager/has_many.rb', line 28
def group_by_key
@metadata.primary_key
end
|
#key ⇒ Object
32
33
34
|
# File 'lib/mongoid/relations/eager/has_many.rb', line 32
def key
@metadata.foreign_key
end
|
#preload ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/mongoid/relations/eager/has_many.rb', line 8
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) ⇒ Object
24
25
26
|
# File 'lib/mongoid/relations/eager/has_many.rb', line 24
def set_relation(doc, element)
doc.__build__(@metadata.name, element, @metadata) unless doc.blank?
end
|