Class: Mongoid::Relations::Eager::HasAndBelongsToMany
- Inherits:
-
Base
- Object
- Base
- Mongoid::Relations::Eager::HasAndBelongsToMany
show all
- Defined in:
- lib/mongoid/relations/eager/has_and_belongs_to_many.rb
Instance Method Summary
collapse
Methods inherited from Base
#each_loaded_document, #grouped_docs, #initialize, #run, #set_on_parent, #shift_metadata
Instance Method Details
#group_by_key ⇒ Object
37
38
39
|
# File 'lib/mongoid/relations/eager/has_and_belongs_to_many.rb', line 37
def group_by_key
@metadata.foreign_key
end
|
#key ⇒ Object
41
42
43
|
# File 'lib/mongoid/relations/eager/has_and_belongs_to_many.rb', line 41
def key
@metadata.primary_key
end
|
#keys_from_docs ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/mongoid/relations/eager/has_and_belongs_to_many.rb', line 25
def keys_from_docs
keys = Set.new
@docs.each do |d|
keys += d.send(group_by_key)
end
keys.to_a
end
|
#preload ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/mongoid/relations/eager/has_and_belongs_to_many.rb', line 8
def preload
@docs.each do |d|
set_relation(d, [])
end
entries = {}
each_loaded_document do |doc|
entries[doc.send(key)] = doc
end
@docs.each do |d|
keys = d.send(group_by_key)
docs = entries.values_at(*keys)
set_relation(d, docs)
end
end
|
#set_relation(doc, element) ⇒ Object
33
34
35
|
# File 'lib/mongoid/relations/eager/has_and_belongs_to_many.rb', line 33
def set_relation(doc, element)
doc.__build__(@metadata.name, element, @metadata)
end
|