From 4b4a85515bbae4ec88547727a5f7f17bc5cefdce Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 13 Jul 2012 11:44:35 +0100 Subject: support relations created with a table alias --- Gemfile | 2 +- activerecord/lib/active_record/relation/query_methods.rb | 2 +- activerecord/test/cases/relations_test.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 1c378c7a68..0ce4541074 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ gemspec if ENV['AREL'] gem 'arel', path: ENV['AREL'] else - gem 'arel' + gem 'arel', github: 'rails/arel' end gem 'mocha', '>= 0.11.2', :require => false diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index e401ed37b0..c567cc7cc4 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -552,7 +552,7 @@ module ActiveRecord end def build_arel - arel = table.from table + arel = Arel::SelectManager.new(table.engine, table) build_joins(arel, joins_values) unless joins_values.empty? diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 8713b8d5e4..c8da7ddd99 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1332,4 +1332,14 @@ class RelationTest < ActiveRecord::TestCase assert_equal expected, relation.inspect end end + + test 'using a custom table affects the wheres' do + table_alias = Post.arel_table.alias('omg_posts') + + relation = ActiveRecord::Relation.new Post, table_alias + relation.where!(:foo => "bar") + + node = relation.arel.constraints.first.grep(Arel::Attributes::Attribute).first + assert_equal table_alias, node.relation + end end -- cgit v1.2.3