diff options
-rw-r--r-- | lib/arel/engines/sql/relations/table.rb | 2 | ||||
-rw-r--r-- | spec/arel/engines/sql/unit/relations/table_spec.rb | 19 |
2 files changed, 20 insertions, 1 deletions
diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb index 6ad294dc6c..0634436280 100644 --- a/lib/arel/engines/sql/relations/table.rb +++ b/lib/arel/engines/sql/relations/table.rb @@ -11,7 +11,7 @@ module Arel if options.is_a?(Hash) @options = options @engine = options[:engine] || Table.engine - @table_alias = options[:as].to_s if options[:as].present? + @table_alias = options[:as].to_s if options[:as].present? && options[:as].to_s != @name else @engine = options # Table.new('foo', engine) end diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb index 977eb343e3..d8584ccaec 100644 --- a/spec/arel/engines/sql/unit/relations/table_spec.rb +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -44,6 +44,25 @@ module Arel }) end end + + it "does not apply alias if it's same as the table name" do + sql = @relation.as(:users).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `users`.`id`, `users`.`name` + FROM `users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "users"."id", "users"."name" + FROM "users" + }) + end + end + end describe '#column_for' do |