Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow using non-table alias as a rhs relation name, fix for #84 and #59 #90

Merged
merged 3 commits into from

3 participants

JoelJuliano Aaron Patterson JoelJuliano-duplicate
JoelJuliano

We have a similar situation as reported in issue #84 (#84)
where in our arel statement we have a nested join using a non-table alias and it's throwing an exception
"undefined method `name' for #Arel::Nodes::Grouping:0x00000123456789"

Aaron Patterson
Owner

Can you write a failing test to go along with this please?

JoelJuliano

I've added the failing test

Aaron Patterson tenderlove merged commit 411336b into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 1 deletion.
  1. +1 −1  lib/arel/nodes/table_alias.rb
  2. +17 −0 test/test_select_manager.rb
2  lib/arel/nodes/table_alias.rb
View
@@ -10,7 +10,7 @@ def [] name
end
def table_name
- relation.name
+ relation.respond_to?(:name) ? relation.name : name
end
end
end
17 test/test_select_manager.rb
View
@@ -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
Something went wrong with that request. Please try again.