Skip to content

Commit

Permalink
Changing AR::AssociationPreload::ClassMethods#preload_associations to…
Browse files Browse the repository at this point in the history
… correctly preload associations on mixed groups of parents.
  • Loading branch information
Andrew Chase committed Jun 28, 2008
1 parent 67e8ec0 commit e9b955e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions activerecord/lib/active_record/association_preload.rb
Expand Up @@ -10,6 +10,8 @@ module ClassMethods
# preload_options is passed only one level deep: don't pass to the child associations when associations is a Hash
protected
def preload_associations(records, associations, preload_options={})
logger.debug("!!!!!!!!!!!!!!!!!!!! ACC preload_associations in class #{self.name}")
logger.debug("associations: #{associations.inspect}")
records = [records].flatten.compact.uniq
return if records.empty?
case associations
Expand All @@ -22,7 +24,10 @@ def preload_associations(records, associations, preload_options={})
reflection = reflections[parent]
parents = records.map {|record| record.send(reflection.name)}.flatten
unless parents.empty? || parents.first.nil?
parents.first.class.preload_associations(parents, child)
class_to_reflection = {}
parents.group_by(&:class).each do |parent_class, grouped_parents|
parent_class.preload_associations(grouped_parents,child)
end
end
end
end
Expand All @@ -36,7 +41,7 @@ def preload_one_association(records, association, preload_options={})
# group on the reflection itself so that if various subclass share the same association then we do not split them
# unncessarily
records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records|
raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
raise ConfigurationError, "Association named \'#{ association }\' was not found; perhaps you misspelled it?" unless reflection
send("preload_#{reflection.macro}_association", records, reflection, preload_options)
end
end
Expand All @@ -58,6 +63,7 @@ def add_preloaded_record_to_collection(parent_records, reflection_name, associat
end

def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key)
logger.debug("!!!!!!!!!!!!!!!!!!!! ACC set_association_collection_records #{reflection_name} #{key}")
associated_records.each do |associated_record|
mapped_records = id_to_record_map[associated_record[key].to_s]
add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record)
Expand Down Expand Up @@ -181,6 +187,7 @@ def preload_through_records(records, reflection, through_association)
end
through_records.flatten!
else
logger.debug("!!!!!!!!!!!!!!!!!!!! ACC preload_associations using #{records.first.class.name}")
records.first.class.preload_associations(records, through_association)
through_records = records.map {|record| record.send(through_association)}.flatten
end
Expand Down Expand Up @@ -239,11 +246,14 @@ def preload_belongs_to_association(records, reflection, preload_options={})
end

def find_associated_records(ids, reflection, preload_options)
logger.debug("!!!!!!!!!!!!!!!!!!!! ACC find_associated_records #{reflection.inspect}\n#{preload_options.inspect}")
options = reflection.options
table_name = reflection.klass.quoted_table_name

if interface = reflection.options[:as]
# interface_class = reflection.active_record
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}'"
# 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}'"
else
foreign_key = reflection.primary_key_name
conditions = "#{reflection.klass.quoted_table_name}.#{foreign_key} IN (?)"
Expand Down

0 comments on commit e9b955e

Please sign in to comment.