Skip to content

Commit

Permalink
Merge pull request #35360 from kamipo/fix_select_and_pluck_with_from
Browse files Browse the repository at this point in the history
Fix `pluck` and `select` with `from` if `from` has original table name
  • Loading branch information
kamipo committed Feb 21, 2019
2 parents 3b6602a + 04a4789 commit 4c68fd2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,10 @@ def arel_columns(columns)

def arel_column(field)
field = klass.attribute_alias(field) if klass.attribute_alias?(field)
from = from_clause.name || from_clause.value

if klass.columns_hash.key?(field) && !from_clause.value
if klass.columns_hash.key?(field) &&
(!from || from == table.name || from == connection.quote_table_name(table.name))
arel_attribute(field)
else
yield
Expand Down
39 changes: 39 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,45 @@ def test_finding_with_subquery_without_select_does_not_change_the_select
end
end

def test_select_with_original_table_name_in_from
relation = Comment.joins(:post).select(:id).order(:id)
subquery = Comment.from(Comment.table_name).joins(:post).select(:id).order(:id)
assert_equal relation.map(&:id), subquery.map(&:id)
end

def test_pluck_with_original_table_name_in_from
relation = Comment.joins(:post).order(:id)
subquery = Comment.from(Comment.table_name).joins(:post).order(:id)
assert_equal relation.pluck(:id), subquery.pluck(:id)
end

def test_select_with_quoted_original_table_name_in_from
relation = Comment.joins(:post).select(:id).order(:id)
subquery = Comment.from(Comment.quoted_table_name).joins(:post).select(:id).order(:id)
assert_equal relation.map(&:id), subquery.map(&:id)
end

def test_pluck_with_quoted_original_table_name_in_from
relation = Comment.joins(:post).order(:id)
subquery = Comment.from(Comment.quoted_table_name).joins(:post).order(:id)
assert_equal relation.pluck(:id), subquery.pluck(:id)
end

def test_select_with_subquery_in_from_uses_original_table_name
if current_adapter?(:SQLite3Adapter) && ENV["TRAVIS"]
skip "https://travis-ci.org/rails/rails/jobs/496726410#L1198-L1208"
end
relation = Comment.joins(:post).select(:id).order(:id)
subquery = Comment.from(Comment.all, Comment.quoted_table_name).joins(:post).select(:id).order(:id)
assert_equal relation.map(&:id), subquery.map(&:id)
end

def test_pluck_with_subquery_in_from_uses_original_table_name
relation = Comment.joins(:post).order(:id)
subquery = Comment.from(Comment.all, Comment.quoted_table_name).joins(:post).order(:id)
assert_equal relation.pluck(:id), subquery.pluck(:id)
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")
Expand Down

0 comments on commit 4c68fd2

Please sign in to comment.