Skip to content

Commit 849dec4

Browse files
author
Rick Song
committed
Makes a minor fix to PredicateBuilder respect custom primary keys when
calling `Relation#where`
1 parent 00545f3 commit 849dec4

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Fixes custom primary keys for associations when calling `Relation#where`
2+
3+
Fixes #23327.
4+
5+
*Rick Song*
6+
17
* No longer pass deprecated option `-i` to `pg_dump`.
28

39
*Paul Sadauskas*

activerecord/lib/active_record/relation/predicate_builder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def self.expand(klass, table, column, value)
5656
# For polymorphic relationships, find the foreign key and type:
5757
# PriceEstimate.where(estimate_of: treasure)
5858
if klass && reflection = klass._reflect_on_association(column.to_sym)
59-
if reflection.polymorphic? && base_class = polymorphic_base_class_from_value(value)
59+
base_class = polymorphic_base_class_from_value(value)
60+
if reflection.polymorphic? && base_class
6061
queries << build(table[reflection.foreign_type], base_class)
6162
end
6263

activerecord/test/cases/relation/where_test.rb

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "models/author"
33
require "models/binary"
44
require "models/cake_designer"
5+
require "models/category"
56
require "models/chef"
67
require "models/comment"
78
require "models/edge"
@@ -14,7 +15,7 @@
1415

1516
module ActiveRecord
1617
class WhereTest < ActiveRecord::TestCase
17-
fixtures :posts, :edges, :authors, :binaries, :essays
18+
fixtures :posts, :edges, :authors, :categories, :binaries, :essays
1819

1920
def test_where_copies_bind_params
2021
author = authors(:david)
@@ -207,35 +208,70 @@ def test_where_with_blank_conditions
207208
end
208209

209210
def test_where_on_association_with_custom_primary_key
211+
category = categories(:general)
212+
essay = Essay.where(category: category).first
213+
214+
assert_equal essays(:david_modest_proposal), essay
215+
end
216+
217+
def test_where_on_association_with_custom_primary_key_with_relation
218+
category = categories(:general)
219+
essay = Essay.where(category: Category.where(id: category.id)).first
220+
221+
assert_equal essays(:david_modest_proposal), essay
222+
end
223+
224+
def test_where_on_association_with_relation_performs_subselect_not_two_queries
225+
category = categories(:general)
226+
227+
assert_queries(1) do
228+
Essay.where(category: Category.where(id: category.id)).to_a
229+
end
230+
end
231+
232+
def test_where_on_association_with_custom_primary_key_with_array_of_base
233+
category = categories(:general)
234+
essay = Essay.where(category: [category]).first
235+
236+
assert_equal essays(:david_modest_proposal), essay
237+
end
238+
239+
def test_where_on_association_with_custom_primary_key_with_array_of_ids
240+
essay = Essay.where(category: ["General"]).first
241+
242+
assert_equal essays(:david_modest_proposal), essay
243+
end
244+
245+
def test_where_on_polymorphic_association_with_custom_primary_key
210246
author = authors(:david)
211247
essay = Essay.where(writer: author).first
212248

213249
assert_equal essays(:david_modest_proposal), essay
214250
end
215251

216-
def test_where_on_association_with_custom_primary_key_with_relation
252+
def test_where_on_polymorphic_association_with_custom_primary_key_with_relation
217253
author = authors(:david)
218254
essay = Essay.where(writer: Author.where(id: author.id)).first
219255

220256
assert_equal essays(:david_modest_proposal), essay
221257
end
222258

223-
def test_where_on_association_with_relation_performs_subselect_not_two_queries
259+
def test_where_on_polymorphic_association_with_relation_performs_subselect_not_two_queries
224260
author = authors(:david)
225261

226262
assert_queries(1) do
227263
Essay.where(writer: Author.where(id: author.id)).to_a
228264
end
229265
end
230266

231-
def test_where_on_association_with_custom_primary_key_with_array_of_base
267+
def test_where_on_polymorphic_association_with_custom_primary_key_with_array_of_base
232268
author = authors(:david)
233269
essay = Essay.where(writer: [author]).first
234270

235271
assert_equal essays(:david_modest_proposal), essay
236272
end
237273

238-
def test_where_on_association_with_custom_primary_key_with_array_of_ids
274+
def test_where_on_polymorphic_association_with_custom_primary_key_with_array_of_ids
239275
essay = Essay.where(writer: ["David"]).first
240276

241277
assert_equal essays(:david_modest_proposal), essay

0 commit comments

Comments
 (0)