Skip to content

Commit 3e28346

Browse files
authored
Merge pull request rails#53264 from kamipo/fix_pluck_with_qualified_name_on_loaded
Fix `pluck` with qualified name on loaded relation
2 parents 4e6225d + 03d949b commit 3e28346

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ def pluck(*column_names)
311311
relation.pluck(*column_names)
312312
else
313313
model.disallow_raw_sql!(flattened_args(column_names))
314-
columns = arel_columns(column_names)
315314
relation = spawn
315+
columns = relation.arel_columns(column_names)
316316
relation.select_values = columns
317317
result = skip_query_cache_if_necessary do
318318
if where_clause.contradiction?

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,22 @@ def async!
16561656
self
16571657
end
16581658

1659+
protected
1660+
def arel_columns(columns)
1661+
columns.flat_map do |field|
1662+
case field
1663+
when Symbol, String
1664+
arel_column(field)
1665+
when Proc
1666+
field.call
1667+
when Hash
1668+
arel_columns_from_hash(field)
1669+
else
1670+
field
1671+
end
1672+
end
1673+
end
1674+
16591675
private
16601676
def async
16611677
spawn.async!
@@ -1943,21 +1959,6 @@ def build_with_join_node(name, kind = Arel::Nodes::InnerJoin)
19431959
).join_sources.first
19441960
end
19451961

1946-
def arel_columns(columns)
1947-
columns.flat_map do |field|
1948-
case field
1949-
when Symbol, String
1950-
arel_column(field)
1951-
when Proc
1952-
field.call
1953-
when Hash
1954-
arel_columns_from_hash(field)
1955-
else
1956-
field
1957-
end
1958-
end
1959-
end
1960-
19611962
def arel_columns_from_hash(fields)
19621963
fields.flat_map do |table_name, columns|
19631964
table_name = table_name.name if table_name.is_a?(Symbol)

activerecord/test/cases/calculations_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,18 @@ def test_pluck_replaces_select_clause
12411241
assert_equal [false, true, true, true, true], takes_relation.pluck(:approved)
12421242
end
12431243

1244+
def test_pluck_with_qualified_name_on_loaded
1245+
topics = Topic.joins(:replies).order(:id)
1246+
1247+
assert_not_predicate topics, :loaded?
1248+
assert_equal [[1, 2], [3, 4]], topics.pluck("topics.id", "replies.id")
1249+
1250+
topics.load
1251+
1252+
assert_predicate topics, :loaded?
1253+
assert_equal [[1, 2], [3, 4]], topics.pluck("topics.id", "replies.id")
1254+
end
1255+
12441256
def test_pluck_columns_with_same_name
12451257
expected = [["The First Topic", "The Second Topic of the day"], ["The Third Topic of the day", "The Fourth Topic of the day"]]
12461258
actual = Topic.joins(:replies).order(:id)

0 commit comments

Comments
 (0)