From b9e90e4e55290172d7c5918319fd5fe35aa6a10e Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 5 May 2008 11:22:48 -0700 Subject: better column disambiguation --- lib/arel/primitives/attribute.rb | 8 +++++--- lib/arel/relations/join.rb | 9 ++++++--- lib/arel/relations/nil.rb | 1 + lib/arel/relations/relation.rb | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 14b8c18cb2..5895f7e9b9 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -1,3 +1,5 @@ +require 'set' + module Arel class Attribute attr_reader :relation, :name, :alias, :ancestor @@ -67,9 +69,9 @@ module Arel !(history & other.history).empty? end - def %(other) - if other then (history - other.history) + (other.history - history) - else history + def /(other) + if other then (history & other.history).size.to_f / Set.new(history + other.history).size + else 0 end end end diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index 4269731092..5356e57394 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -44,9 +44,12 @@ module Arel end def relation_for(attribute) - externalize([relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| - (attribute % a1).size <=> (attribute % a2).size - end.relation).relation_for(attribute) + x = [externalize(relation1), externalize(relation2)].max do |r1, r2| + o1, o2 = r1.relation_for(attribute), r2.relation_for(attribute) + a1, a2 = o1 && o1[attribute], o2 && o2[attribute] + + attribute / a1 <=> attribute / a2 + end.relation_for(attribute) end private diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb index 258f60f478..b0d97dd174 100644 --- a/lib/arel/relations/nil.rb +++ b/lib/arel/relations/nil.rb @@ -2,6 +2,7 @@ module Arel class Nil < Relation def table; self end def table_sql(formatter = nil); '' end + def relation_for(attribute); nil end def name; '' end def to_s; '' end diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index cc0f40ea17..8c56a0cdcc 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -140,8 +140,8 @@ module Arel end def attribute_for_attribute(attribute) - attributes.select { |a| a =~ attribute }.min do |a1, a2| - (attribute % a1).size <=> (attribute % a2).size + attributes.select { |a| a =~ attribute }.max do |a1, a2| + (attribute / a1) <=> (attribute / a2) end end end -- cgit v1.2.3