Skip to content

Commit

Permalink
Merge pull request #90 from JoelJuliano/patch-1
Browse files Browse the repository at this point in the history
Allow using non-table alias as a rhs relation name, fix for #84 and #59
  • Loading branch information
tenderlove committed Oct 31, 2011
2 parents 8ab89f5 + 2680d64 commit 411336b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/arel/nodes/table_alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def [] name
end

def table_name
relation.name
relation.respond_to?(:name) ? relation.name : name
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/test_select_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,23 @@ def test_join_sources
}
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
Expand Down

0 comments on commit 411336b

Please sign in to comment.