public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/glyde/rails.git
Changing AR::AssociationPreload::ClassMethods#preload_associations to correctly 
preload associations on mixed groups of parents.
Andrew Chase (author)
Fri Jun 27 17:17:19 -0700 2008
commit  e9b955eff8f420ebd842db827260708a23e3347a
tree    c1bcffd3d956840f741616bb8a3e5b30ed52e76a
parent  67e8ec0e07314d367b1dc19ed3a1edf05105f8ce
...
10
11
12
 
 
13
14
15
...
22
23
24
25
 
 
 
 
26
27
28
...
36
37
38
39
 
40
41
42
...
58
59
60
 
61
62
63
...
181
182
183
 
184
185
186
...
239
240
241
 
242
243
244
245
 
246
 
247
248
249
...
10
11
12
13
14
15
16
17
...
24
25
26
 
27
28
29
30
31
32
33
...
41
42
43
 
44
45
46
47
...
63
64
65
66
67
68
69
...
187
188
189
190
191
192
193
...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
0
@@ -10,6 +10,8 @@ module ActiveRecord
0
       # preload_options is passed only one level deep: don't pass to the child associations when associations is a Hash
0
       protected
0
       def preload_associations(records, associations, preload_options={})
0
+        logger.debug("!!!!!!!!!!!!!!!!!!!! ACC preload_associations in class #{self.name}")
0
+        logger.debug("associations: #{associations.inspect}")
0
         records = [records].flatten.compact.uniq
0
         return if records.empty?
0
         case associations
0
@@ -22,7 +24,10 @@ module ActiveRecord
0
             reflection = reflections[parent]
0
             parents = records.map {|record| record.send(reflection.name)}.flatten
0
             unless parents.empty? || parents.first.nil?
0
-              parents.first.class.preload_associations(parents, child)
0
+              class_to_reflection = {}
0
+              parents.group_by(&:class).each do |parent_class, grouped_parents|
0
+                parent_class.preload_associations(grouped_parents,child)
0
+              end
0
             end
0
           end
0
         end
0
@@ -36,7 +41,7 @@ module ActiveRecord
0
         # group on the reflection itself so that if various subclass share the same association then we do not split them
0
         # unncessarily
0
         records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records|
0
-          raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
0
+          raise ConfigurationError, "Association named \'#{ association }\' was not found; perhaps you misspelled it?" unless reflection
0
           send("preload_#{reflection.macro}_association", records, reflection, preload_options)
0
         end
0
       end
0
@@ -58,6 +63,7 @@ module ActiveRecord
0
       end
0
 
0
       def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key)
0
+        logger.debug("!!!!!!!!!!!!!!!!!!!! ACC set_association_collection_records #{reflection_name} #{key}")
0
         associated_records.each do |associated_record|
0
           mapped_records = id_to_record_map[associated_record[key].to_s]
0
           add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record)
0
@@ -181,6 +187,7 @@ module ActiveRecord
0
           end
0
           through_records.flatten!
0
         else
0
+          logger.debug("!!!!!!!!!!!!!!!!!!!! ACC preload_associations using #{records.first.class.name}")
0
           records.first.class.preload_associations(records, through_association)
0
           through_records = records.map {|record| record.send(through_association)}.flatten
0
         end
0
@@ -239,11 +246,14 @@ module ActiveRecord
0
       end
0
 
0
       def find_associated_records(ids, reflection, preload_options)
0
+        logger.debug("!!!!!!!!!!!!!!!!!!!! ACC find_associated_records #{reflection.inspect}\n#{preload_options.inspect}")
0
         options = reflection.options
0
         table_name = reflection.klass.quoted_table_name
0
 
0
         if interface = reflection.options[:as]
0
+          # interface_class = reflection.active_record
0
           conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} IN (?) and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{self.base_class.name.demodulize}'"
0
+          # conditions = "#{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_id"} IN (?) and #{reflection.klass.quoted_table_name}.#{connection.quote_column_name "#{interface}_type"} = '#{interface_class.name.demodulize}'"
0
         else
0
           foreign_key = reflection.primary_key_name
0
           conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} IN (?)"

Comments