From 45f87174d11dc549a059b7a041172db25bcc32ce Mon Sep 17 00:00:00 2001 From: JoelJuliano Date: Sun, 30 Oct 2011 19:06:15 -0500 Subject: Allow using non-table alias as a rhs relation name, fix for #84 and #59 --- lib/arel/nodes/table_alias.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/arel/nodes/table_alias.rb b/lib/arel/nodes/table_alias.rb index 6ec17885fb..b32f057117 100644 --- a/lib/arel/nodes/table_alias.rb +++ b/lib/arel/nodes/table_alias.rb @@ -10,7 +10,7 @@ module Arel end def table_name - relation.name + relation.respond_to?(:name) ? relation.name : name end end end -- cgit v1.2.3 From d75a0ef0d5139348b7b4d046374502824254a076 Mon Sep 17 00:00:00 2001 From: Joel Bryan Juliano Date: Mon, 31 Oct 2011 00:42:22 -0500 Subject: Added a failing test for a non-table alias as rhs relation name --- test/test_select_manager.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 7e9099d0d0..ca47b873a2 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -654,6 +654,24 @@ module Arel } end + it 'can have a non-table alias as relation name' do + users = Table.new :users + comments = Table.new :comments + + counts = comments.from(comments). + group(comments[:user_id]). + project( + comments[:user_id].as("user_id"), + comments[:user_id].count.as("count") + ).as("counts") + + joins = users.join(counts).on(counts[:user_id].eq(10)) + joins.to_sql.must_be_like %{ + SELECT FROM "users" INNER JOIN (SELECT "comments"."user_id" AS user_id, COUNT("comments"."user_id") AS count FROM "comments" GROUP BY "comments"."user_id") counts ON counts."user_id" = 10 + + } + end + it 'returns string join sql' do table = Table.new :users manager = Arel::SelectManager.new Table.engine -- cgit v1.2.3 From 2680d6485ae79f7232f032e3209a371e17c76191 Mon Sep 17 00:00:00 2001 From: Joel Bryan Juliano Date: Mon, 31 Oct 2011 00:47:18 -0500 Subject: Removed trailing space on the test sql string statement. --- test/test_select_manager.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index ca47b873a2..a00d1843ba 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -668,7 +668,6 @@ module Arel joins = users.join(counts).on(counts[:user_id].eq(10)) joins.to_sql.must_be_like %{ SELECT FROM "users" INNER JOIN (SELECT "comments"."user_id" AS user_id, COUNT("comments"."user_id") AS count FROM "comments" GROUP BY "comments"."user_id") counts ON counts."user_id" = 10 - } end -- cgit v1.2.3