diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-14 20:33:23 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-05-14 20:33:23 -0700 |
commit | 9376459a7186b794b29e11c55186135004e8f3b8 (patch) | |
tree | 37b2905882277e25be9c6d1940be5bc625724711 | |
parent | 16730fdbd0ab630320aba225314aa6a1a3b450fe (diff) | |
download | rails-9376459a7186b794b29e11c55186135004e8f3b8.tar.gz rails-9376459a7186b794b29e11c55186135004e8f3b8.tar.bz2 rails-9376459a7186b794b29e11c55186135004e8f3b8.zip |
fixed defect with select inside of alias joined to the same table (yikes)
-rw-r--r-- | lib/arel/primitives/attribute.rb | 8 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/selection.rb | 2 | ||||
-rw-r--r-- | spec/arel/unit/relations/join_spec.rb | 23 |
4 files changed, 23 insertions, 14 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 5f8618ddfc..22ee37524e 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -47,11 +47,7 @@ module Arel module Transformations def self.included(klass) - alias_method :eql?, :== - end - - def hash - name.hash + history.size.hash + klass.hash_on :name end def as(aliaz = nil) @@ -70,7 +66,7 @@ module Arel module Congruence def history - @history ||= [self] + (ancestor ? [ancestor, ancestor.history].flatten : []) + @history ||= [self] + (ancestor ? ancestor.history : []) end def match?(other) diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index b7c5c3f39b..14903cf01d 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -21,7 +21,7 @@ module Arel join_sql, externalize(relation2).table_sql(formatter), ("ON" unless predicates.blank?), - predicates.collect { |p| p.bind(environment).to_sql }.join(' AND ') + (predicates + externalize(relation2).selects).collect { |p| p.bind(environment).to_sql }.join(' AND ') ].compact.join(" ") [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") end @@ -32,7 +32,7 @@ module Arel end def selects - (externalize(relation1).selects + externalize(relation2).selects).collect { |s| s.bind(self) } + (externalize(relation1).selects).collect { |s| s.bind(self) } end def table diff --git a/lib/arel/relations/selection.rb b/lib/arel/relations/selection.rb index 38a40e1b76..0e7615a83c 100644 --- a/lib/arel/relations/selection.rb +++ b/lib/arel/relations/selection.rb @@ -15,7 +15,7 @@ module Arel end def selects - relation.selects + [predicate] + (relation.selects + [predicate]).collect { |p| p.bind(self) } end end end
\ No newline at end of file diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index d517da8c1f..b5c5dc8e33 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -56,14 +56,13 @@ module Arel ") end - it 'manufactures sql joining the two tables, merging any selects' do + it 'manufactures sql joining the two tables, with selects from the right table in the ON clause' do Join.new("INNER JOIN", @relation1.select(@relation1[:id].eq(1)), @relation2.select(@relation2[:id].eq(2)), @predicate).to_sql.should be_like(" SELECT `users`.`id`, `users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` FROM `users` - INNER JOIN `photos` ON `users`.`id` = `photos`.`user_id` + INNER JOIN `photos` ON `users`.`id` = `photos`.`user_id` AND `photos`.`id` = 2 WHERE `users`.`id` = 1 - AND `photos`.`id` = 2 ") end end @@ -158,10 +157,24 @@ module Arel SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` FROM `users` INNER JOIN `users` AS `users_2` - ON `users`.`id` = `users_2`.`id` - WHERE `users_2`.`id` = 1 + ON `users`.`id` = `users_2`.`id` AND `users_2`.`id` = 1 ") end + + describe 'when the selection occurs before the alias' do + it 'manufactures sql aliasing the predicates properly' do + aliased_relation = @relation1.select(@relation1[:id].eq(1)).alias + @relation1 \ + .join(aliased_relation) \ + .on(aliased_relation[:id].eq(@relation1[:id])) \ + .to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name` + FROM `users` + INNER JOIN `users` AS `users_2` + ON `users_2`.`id` = `users`.`id` AND `users_2`.`id` = 1 + ") + end + end end describe 'when joining the relation to itself multiple times' do |