Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activerecord/lib/active_record/association_preload.rb

Revision 9094, 11.6 kB (checked in by rick, 7 months ago)

More efficient association preloading code that compacts a through_records array in a central location. Closes #11427 [danger]

Line 
1 module ActiveRecord
2   module AssociationPreload #:nodoc:
3     def self.included(base)
4       base.extend(ClassMethods)
5     end
6
7     module ClassMethods
8
9       # Loads the named associations for the activerecord record (or records) given
10       # preload_options is passed only one level deep: don't pass to the child associations when associations is a Hash
11       protected
12       def preload_associations(records, associations, preload_options={})
13         records = [records].flatten.compact.uniq
14         return if records.empty?
15         case associations
16         when Array then associations.each {|association| preload_associations(records, association, preload_options)}
17         when Symbol, String then preload_one_association(records, associations.to_sym, preload_options)
18         when Hash then
19           associations.each do |parent, child|
20             raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
21             preload_associations(records, parent, preload_options)
22             reflection = reflections[parent]
23             parents = records.map {|record| record.send(reflection.name)}.flatten
24             unless parents.empty? || parents.first.nil?
25               parents.first.class.preload_associations(parents, child)
26             end
27           end
28         end
29       end
30
31       private
32
33       def preload_one_association(records, association, preload_options={})
34         reflection = reflections[association]
35         raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
36
37         # Not all records have the same class, so group then preload.
38         records.group_by(&:class).each do |klass, records|
39           reflection = klass.reflections[association]
40           send("preload_#{reflection.macro}_association", records, reflection, preload_options)
41         end
42       end
43
44       def add_preloaded_records_to_collection(parent_records, reflection_name, associated_record)
45         parent_records.each do |parent_record|
46           association_proxy = parent_record.send(reflection_name)
47           association_proxy.loaded
48           association_proxy.target.push(*[associated_record].flatten)
49         end
50       end
51      
52       def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
53         parent_records.each do |parent_record|
54           association_proxy = parent_record.send(reflection_name)
55           association_proxy.loaded
56           association_proxy.target = associated_record
57         end
58       end
59
60       def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key)
61         associated_records.each do |associated_record|
62           mapped_records = id_to_record_map[associated_record[key].to_i]
63           add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record)
64         end
65       end
66
67       def set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
68         associated_records.each do |associated_record|
69           mapped_records = id_to_record_map[associated_record[key].to_i]
70           mapped_records.each do |mapped_record|
71             mapped_record.send("set_#{reflection_name}_target", associated_record)
72           end
73         end
74       end
75
76       def construct_id_map(records)
77         id_to_record_map = {}
78         ids = []
79         records.each do |record|
80           ids << record.id
81           mapped_records = (id_to_record_map[record.id] ||= [])
82           mapped_records << record
83         end
84         ids.uniq!
85         return id_to_record_map, ids
86       end
87
88       # FIXME: quoting
89       def preload_has_and_belongs_to_many_association(records, reflection, preload_options={})
90         table_name = reflection.klass.table_name
91         id_to_record_map, ids = construct_id_map(records)
92         records.each {|record| record.send(reflection.name).loaded}
93         options = reflection.options
94
95         conditions = "t0.#{reflection.primary_key_name}  IN (?)"
96         conditions << append_conditions(options, preload_options)
97
98         associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
99         :include => options[:include],
100         :joins => "INNER JOIN #{options[:join_table]} as t0 ON #{reflection.klass.table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
101         :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as _parent_record_id",
102         :order => options[:order])
103
104         set_association_collection_records(id_to_record_map, reflection.name, associated_records, '_parent_record_id')
105       end
106
107       def preload_has_one_association(records, reflection, preload_options={})
108         id_to_record_map, ids = construct_id_map(records)       
109         options = reflection.options
110         if options[:through]
111           records.each {|record| record.send(reflection.name) && record.send(reflection.name).loaded}
112           through_records = preload_through_records(records, reflection, options[:through])
113           through_reflection = reflections[options[:through]]
114           through_primary_key = through_reflection.primary_key_name
115           unless through_records.empty?
116             source = reflection.source_reflection.name
117             through_records.first.class.preload_associations(through_records, source)
118             through_records.each do |through_record|
119               add_preloaded_record_to_collection(id_to_record_map[through_record[through_primary_key].to_i],
120                                                  reflection.name, through_record.send(source))
121             end
122           end
123         else
124           records.each {|record| record.send("set_#{reflection.name}_target", nil)}
125
126
127           set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
128         end
129       end
130
131       def preload_has_many_association(records, reflection, preload_options={})
132         id_to_record_map, ids = construct_id_map(records)
133         records.each {|record| record.send(reflection.name).loaded}
134         options = reflection.options
135
136         if options[:through]
137           through_records = preload_through_records(records, reflection, options[:through])
138           through_reflection = reflections[options[:through]]
139           through_primary_key = through_reflection.primary_key_name
140           unless through_records.empty?
141             source = reflection.source_reflection.name
142             through_records.first.class.preload_associations(through_records, source)
143             through_records.each do |through_record|
144               add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_i],
145                                                  reflection.name, through_record.send(source))
146             end
147           end
148         else
149           set_association_collection_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options),
150                                              reflection.primary_key_name)
151         end
152       end
153      
154       def preload_through_records(records, reflection, through_association)
155         through_reflection = reflections[through_association]
156         through_primary_key = through_reflection.primary_key_name
157
158         if reflection.options[:source_type]
159           interface = reflection.source_reflection.options[:foreign_type]
160           preload_options = {:conditions => ["#{interface} = ?", reflection.options[:source_type]]}
161
162           records.compact!
163           records.first.class.preload_associations(records, through_association, preload_options)
164
165           # Dont cache the association - we would only be caching a subset
166           through_records = []
167           records.each do |record|
168             proxy = record.send(through_association)
169
170             if proxy.respond_to?(:target)
171               through_records << proxy.target
172               proxy.reset
173             else # this is a has_one :through reflection
174               through_records << proxy if proxy
175             end
176           end
177           through_records.flatten!
178         else
179           records.first.class.preload_associations(records, through_association)
180           through_records = records.map {|record| record.send(through_association)}.flatten
181         end
182         through_records.compact!
183         through_records
184       end
185
186       # FIXME: quoting
187       def preload_belongs_to_association(records, reflection, preload_options={})
188         options = reflection.options
189         primary_key_name = reflection.primary_key_name
190
191         if options[:polymorphic]
192           polymorph_type = options[:foreign_type]
193           klasses_and_ids = {}
194
195           # Construct a mapping from klass to a list of ids to load and a mapping of those ids back to their parent_records
196           records.each do |record|
197             if klass = record.send(polymorph_type)
198               klass_id = record.send(primary_key_name)
199
200               id_map = klasses_and_ids[klass] ||= {}
201               id_list_for_klass_id = (id_map[klass_id] ||= [])
202               id_list_for_klass_id << record
203             end
204           end
205           klasses_and_ids = klasses_and_ids.to_a
206         else
207           id_map = {}
208           records.each do |record|
209             mapped_records = (id_map[record.send(primary_key_name)] ||= [])
210             mapped_records << record
211           end
212           klasses_and_ids = [[reflection.klass.name, id_map]]
213         end
214
215         klasses_and_ids.each do |klass_and_id|
216           klass_name, id_map = *klass_and_id
217           klass = klass_name.constantize
218
219           table_name = klass.table_name
220           primary_key = klass.primary_key
221           conditions = "#{table_name}.#{primary_key} IN (?)"
222           conditions << append_conditions(options, preload_options)
223           associated_records = klass.find(:all, :conditions => [conditions, id_map.keys.uniq],
224                                           :include => options[:include],
225                                           :select => options[:select],
226                                           :joins => options[:joins],
227                                           :order => options[:order])
228           set_association_single_records(id_map, reflection.name, associated_records, primary_key)
229         end
230       end
231
232       # FIXME: quoting
233       def find_associated_records(ids, reflection, preload_options)
234         options = reflection.options
235         table_name = reflection.klass.table_name
236
237         if interface = reflection.options[:as]
238           conditions = "#{reflection.klass.table_name}.#{interface}_id IN (?) and #{reflection.klass.table_name}.#{interface}_type = '#{self.base_class.name.demodulize}'"
239         else
240           foreign_key = reflection.primary_key_name
241           conditions = "#{reflection.klass.table_name}.#{foreign_key} IN (?)"
242         end
243
244         conditions << append_conditions(options, preload_options)
245
246         reflection.klass.find(:all,
247                               :select => (options[:select] || "#{table_name}.*"),
248                               :include => options[:include],
249                               :conditions => [conditions, ids],
250                               :joins => options[:joins],
251                               :group => options[:group],
252                               :order => options[:order])
253       end
254
255
256       def interpolate_sql_for_preload(sql)
257         instance_eval("%@#{sql.gsub('@', '\@')}@")
258       end
259
260       def append_conditions(options, preload_options)
261         sql = ""
262         sql << " AND (#{interpolate_sql_for_preload(sanitize_sql(options[:conditions]))})" if options[:conditions]
263         sql << " AND (#{sanitize_sql preload_options[:conditions]})" if preload_options[:conditions]
264         sql
265       end
266
267     end
268   end
269 end
Note: See TracBrowser for help on using the browser.