Skip to content

Commit f060758

Browse files
authored
Merge pull request rails#53089 from kamipo/fix_pluck_with_qualified_schema_name
Fix `pluck` with schema qualified table name
2 parents 9568093 + 4f13927 commit f060758

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,8 +1954,7 @@ def arel_column(field)
19541954

19551955
if model.columns_hash.key?(field) && (!from || table_name_matches?(from))
19561956
table[field]
1957-
elsif field.match?(/\A\w+\.\w+\z/)
1958-
table, column = field.split(".")
1957+
elsif /\A(?<table>(?:\w+\.)?\w+)\.(?<column>\w+)\z/ =~ field
19591958
self.references_values |= [Arel.sql(table, retryable: true)]
19601959
predicate_builder.resolve_arel_attribute(table, column) do
19611960
lookup_table_klass_from_join_dependencies(table)

activerecord/test/cases/adapters/postgresql/schema_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ def test_proper_encoding_of_table_name
292292
assert_equal '"schema.name"."table.name"', @connection.quote_table_name('"schema.name"."table.name"')
293293
end
294294

295+
def test_where_with_qualified_schema_name
296+
Thing1.create(id: 1, name: "thing1", email: "thing1@localhost", moment: Time.now)
297+
assert_equal ["thing1"], Thing1.where("test_schema.things.name": "thing1").map(&:name)
298+
end
299+
300+
def test_pluck_with_qualified_schema_name
301+
Thing1.create(id: 1, name: "thing1", email: "thing1@localhost", moment: Time.now)
302+
assert_equal ["thing1"], Thing1.pluck(:"test_schema.things.name")
303+
end
304+
295305
def test_classes_with_qualified_schema_name
296306
assert_equal 0, Thing1.count
297307
assert_equal 0, Thing2.count

0 commit comments

Comments
 (0)