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

Changeset 8051

Show
Ignore:
Timestamp:
10/29/07 03:02:42 (2 years ago)
Author:
bitsweat
Message:

Associations: speedup duplicate record check. Closes #10011.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8049 r8051  
    11*SVN* 
     2 
     3* Associations: speedup duplicate record check.  #10011 [lifofifo] 
    24 
    35* Make sure that << works on has_many associations on unsaved records.  Closes #9989 [hasmanyjosh] 
  • trunk/activerecord/lib/active_record/associations.rb

    r8046 r8051  
    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 
     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 
    14061433          end 
    14071434 
     
    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