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 1402 1402 end 1403 1403 construct(@base_records_hash[primary_id], @associations, join_associations.dup, row) 1404 1404 end 1405 remove_duplicate_results!(join_base.active_record, @base_records_in_order, @associations) 1405 1406 return @base_records_in_order 1406 1407 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 1407 1434 1408 1435 def aliased_table_names_for(table_name) 1409 1436 joins.select{|join| join.table_name == table_name }.collect{|join| join.aliased_table_name} … … 1461 1488 1462 1489 return nil if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil? 1463 1490 association = join.instantiate(row) 1464 collection.target.push(association) unless collection.target.include?(association)1491 collection.target.push(association) 1465 1492 when :has_one 1466 1493 return if record.id.to_s != join.parent.record_id(row).to_s 1467 1494 association = join.instantiate(row) unless row[join.aliased_primary_key].nil?