1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #18744 from mfazekas/no-table-name-with-from

Fix appending table_name to select and group when used with subquery (fr...
This commit is contained in:
Rafael Mendonça França 2015-02-24 16:55:24 -03:00
parent 2bc72ae29c
commit c479480638
2 changed files with 20 additions and 5 deletions

View file

@ -999,11 +999,15 @@ module ActiveRecord
end end
def arel_columns(columns) def arel_columns(columns)
columns.map do |field| if from_value
if (Symbol === field || String === field) && columns_hash.key?(field.to_s) columns
arel_table[field] else
else columns.map do |field|
field if (Symbol === field || String === field) && columns_hash.key?(field.to_s)
arel_table[field]
else
field
end
end end
end end
end end

View file

@ -157,6 +157,17 @@ class RelationTest < ActiveRecord::TestCase
end end
end end
def test_select_with_subquery_in_from_does_not_use_original_table_name
relation = Comment.group(:type).select('COUNT(post_id) AS post_count, type')
subquery = Comment.from(relation).select('type','post_count')
assert_equal(relation.map(&:post_count).sort,subquery.map(&:post_count).sort)
end
def test_group_with_subquery_in_from_does_not_use_original_table_name
relation = Comment.group(:type).select('COUNT(post_id) AS post_count,type')
subquery = Comment.from(relation).group('type').average("post_count")
assert_equal(relation.map(&:post_count).sort,subquery.values.sort)
end
def test_finding_with_conditions def test_finding_with_conditions
assert_equal ["David"], Author.where(:name => 'David').map(&:name) assert_equal ["David"], Author.where(:name => 'David').map(&:name)