Skip to content

Commit e93a128

Browse files
committed
Preserve duplicate columns in ActiveRecord::Base#select
1 parent e3bc028 commit e93a128

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

activerecord/lib/active_record/relation/merger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ def merge_select_values
8585
return if other.select_values.empty?
8686

8787
if other.model == relation.model
88-
relation.select_values |= other.select_values
88+
relation.select_values += other.select_values
8989
else
90-
relation.select_values |= other.instance_eval do
90+
relation.select_values += other.instance_eval do
9191
arel_columns(select_values)
9292
end
9393
end

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def select(*fields)
426426
end
427427

428428
def _select!(*fields) # :nodoc:
429-
self.select_values |= fields
429+
self.select_values += fields
430430
self
431431
end
432432

activerecord/test/cases/relation/select_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def test_select_with_hash_argument_with_few_tables
115115
assert_not_nil post.post_title
116116
end
117117

118+
def test_select_preserves_duplicate_columns
119+
quoted_posts_id = Regexp.escape(quote_table_name("posts.id"))
120+
quoted_posts = Regexp.escape(quote_table_name("posts"))
121+
assert_queries_match(/SELECT #{quoted_posts_id}, #{quoted_posts_id} FROM #{quoted_posts}/i) do
122+
Post.select(:id, :id).to_a
123+
end
124+
end
125+
118126
def test_reselect
119127
expected = Post.select(:title).to_sql
120128
assert_equal expected, Post.select(:title, :body).reselect(:title).to_sql

activerecord/test/cases/relation_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_multi_values_deduplication_with_merge
5050
}
5151
expected.default = [ Object.new ]
5252

53-
Relation::MULTI_VALUE_METHODS.each do |method|
53+
(Relation::MULTI_VALUE_METHODS - [:select]).each do |method|
5454
getter, setter = "#{method}_values", "#{method}_values="
5555
values = expected[method]
5656
relation = Relation.new(FakeKlass)
@@ -293,6 +293,15 @@ def test_relation_merging_with_joins_as_join_dependency_pick_proper_parent
293293
assert_equal 3, relation.where(id: post.id).pluck(:id).size
294294
end
295295

296+
def test_merge_preserves_duplicate_columns
297+
quoted_posts_id = Regexp.escape(quote_table_name("posts.id"))
298+
quoted_posts = Regexp.escape(quote_table_name("posts"))
299+
posts = Post.select(:id)
300+
assert_queries_match(/SELECT #{quoted_posts_id}, #{quoted_posts_id} FROM #{quoted_posts}/i) do
301+
posts.merge(posts).to_a
302+
end
303+
end
304+
296305
def test_merge_raises_with_invalid_argument
297306
assert_raises ArgumentError do
298307
relation = Relation.new(FakeKlass)

0 commit comments

Comments
 (0)