54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/active_record/turntable/active_record_ext/clever_load.rb', line 54
def clever_load!(association_name)
records = self.to_a
klass = records.first.class
reflection = klass.reflections[association_name]
if reflection
foreign_class = reflection.klass
foreign_objects = case reflection.macro
when :has_one
foreign_class.where(reflection.foreign_key => records.map(&reflection.association_primary_key.to_sym).uniq)
when :belongs_to
foreign_class.where(reflection.association_primary_key => records.map(&reflection.foreign_key.to_sym).uniq)
else
[]
end
self.each do |obj|
matched_object = case reflection.macro
when :has_one
foreign_objects.find {|fo|
obj.send(reflection.association_primary_key) == fo.send(reflection.foreign_key)
}
when :belongs_to
foreign_objects.find {|fo|
obj.send(reflection.foreign_key) == fo.send(reflection.association_primary_key)
}
end
obj.association(association_name).target = matched_object
obj.association(association_name).send(:set_inverse_instance, matched_object)
end
end
records
end
|