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

Ticket #10011: optimize_preloading_init.patch

File optimize_preloading_init.patch, 2.6 kB (added by lifofifo, 10 months ago)
  • activerecord/lib/active_record/associations.rb

    old new  
    14021402              end 
    14031403              construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) 
    14041404            end 
     1405            remove_duplicate_results!(join_base.active_record, @base_records_in_order, @associations) 
    14051406            return @base_records_in_order 
    14061407          end 
     1408           
     1409          def remove_duplicate_results!(base, records, associations) 
     1410            case associations 
     1411              when Symbol, String 
     1412                reflection = base.reflections[associations]  
     1413                if reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro) 
     1414                  records.each { |record| record.send(reflection.name).target.uniq! } 
     1415                end 
     1416              when Array 
     1417                associations.each do |association| 
     1418                  remove_duplicate_results!(base, records, association) 
     1419                end 
     1420              when Hash 
     1421                associations.keys.each do |name| 
     1422                  reflection = base.reflections[name] 
     1423                  is_collection = [:has_many, :has_and_belongs_to_many].include?(reflection.macro) 
     1424                   
     1425                  parent_records = records.map do |record| 
     1426                    next unless record.send(reflection.name) 
     1427                    is_collection ? record.send(reflection.name).target.uniq! : record.send(reflection.name) 
     1428                  end.flatten.compact 
     1429                   
     1430                  remove_duplicate_results!(reflection.class_name.constantize, parent_records, associations[name]) unless parent_records.empty? 
     1431                end 
     1432            end  
     1433          end 
    14071434 
    14081435          def aliased_table_names_for(table_name) 
    14091436            joins.select{|join| join.table_name == table_name }.collect{|join| join.aliased_table_name} 
     
    14611488 
    14621489                  return nil if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil? 
    14631490                  association = join.instantiate(row) 
    1464                   collection.target.push(association) unless collection.target.include?(association) 
     1491                  collection.target.push(association) 
    14651492                when :has_one 
    14661493                  return if record.id.to_s != join.parent.record_id(row).to_s 
    14671494                  association = join.instantiate(row) unless row[join.aliased_primary_key].nil?