Skip to content
This repository
Browse code

Merge pull request #90 from JoelJuliano/patch-1

Allow using non-table alias as a rhs relation name, fix for #84 and #59
  • Loading branch information...
commit 411336b86712df79016c2a4f36fe4e943546a185 2 parents 8ab89f5 + 2680d64
Aaron Patterson tenderlove authored
2  lib/arel/nodes/table_alias.rb
@@ -10,7 +10,7 @@ def [] name
10 10 end
11 11
12 12 def table_name
13   - relation.name
  13 + relation.respond_to?(:name) ? relation.name : name
14 14 end
15 15 end
16 16 end
17 test/test_select_manager.rb
@@ -654,6 +654,23 @@ def test_join_sources
654 654 }
655 655 end
656 656
  657 + it 'can have a non-table alias as relation name' do
  658 + users = Table.new :users
  659 + comments = Table.new :comments
  660 +
  661 + counts = comments.from(comments).
  662 + group(comments[:user_id]).
  663 + project(
  664 + comments[:user_id].as("user_id"),
  665 + comments[:user_id].count.as("count")
  666 + ).as("counts")
  667 +
  668 + joins = users.join(counts).on(counts[:user_id].eq(10))
  669 + joins.to_sql.must_be_like %{
  670 + 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
  671 + }
  672 + end
  673 +
657 674 it 'returns string join sql' do
658 675 table = Table.new :users
659 676 manager = Arel::SelectManager.new Table.engine

0 comments on commit 411336b

Please sign in to comment.
Something went wrong with that request. Please try again.