diff options
-rw-r--r-- | lib/arel/nodes/table_alias.rb | 4 | ||||
-rw-r--r-- | lib/arel/table.rb | 3 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 2 | ||||
-rw-r--r-- | test/test_table.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 9 |
5 files changed, 21 insertions, 1 deletions
diff --git a/lib/arel/nodes/table_alias.rb b/lib/arel/nodes/table_alias.rb index 4f4d5e29e9..1d3d36be0c 100644 --- a/lib/arel/nodes/table_alias.rb +++ b/lib/arel/nodes/table_alias.rb @@ -8,6 +8,10 @@ module Arel def [] name Attribute.new(self, name) end + + def table_name + relation.name + end end end end diff --git a/lib/arel/table.rb b/lib/arel/table.rb index d1d1e40e11..2aff71b220 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -8,6 +8,9 @@ module Arel attr_accessor :name, :engine, :aliases, :table_alias + # TableAlias and Table both have a #table_name which is the name of the underlying table + alias :table_name :name + def initialize name, engine = Table.engine @name = name.to_s @engine = engine diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 7eaaff8e13..f30557e509 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -88,7 +88,7 @@ key on UpdateManager using UpdateManager#key= def column_for attr name = attr.name.to_s - table = attr.relation.name + table = attr.relation.table_name return nil unless table_exists? table diff --git a/test/test_table.rb b/test/test_table.rb index 129d7ba736..1035540513 100644 --- a/test/test_table.rb +++ b/test/test_table.rb @@ -164,6 +164,10 @@ module Arel @relation.name.must_equal 'users' end + it "should have a table name" do + @relation.table_name.must_equal 'users' + end + it "should have an engine" do @relation.engine.must_equal Table.engine end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 3af316037a..c47fd57a28 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -281,6 +281,15 @@ module Arel } end end + + describe 'TableAlias' do + it "should use the underlying table for checking columns" do + test = Table.new(:users).alias('zomgusers')[:id].eq '3' + @visitor.accept(test).must_be_like %{ + "zomgusers"."id" = 3 + } + end + end end end end |