Skip to content

Commit bc9beb2

Browse files
committed
Fix where on polymorphic association with non Active Record object
It is a regression for rails#40815. If `where` on polymorphic association with non Active Record object, `associated_table.join_primary_key` is called without `klass` in `AssociationQueryValue`, it is wrong usage for polymorphic association. Always use `PolymorphicArrayValue` for polymorphic association to avoid the issue. Fixes rails#40937.
1 parent f307197 commit bc9beb2

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

activerecord/lib/active_record/relation/predicate_builder.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,8 @@ def expand_from_hash(attributes, &block)
9393
# PriceEstimate.where(estimate_of: treasure)
9494
associated_table = table.associated_table(key)
9595
if associated_table.polymorphic_association?
96-
case value.is_a?(Array) ? value.first : value
97-
when Base, Relation
98-
value = [value] unless value.is_a?(Array)
99-
klass = PolymorphicArrayValue
100-
end
96+
value = [value] unless value.is_a?(Array)
97+
klass = PolymorphicArrayValue
10198
elsif associated_table.through_association?
10299
next associated_table.predicate_builder.expand_from_hash(
103100
associated_table.primary_key => value

activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def initialize(associated_table, values)
1010

1111
def queries
1212
type_to_ids_mapping.map do |type, ids|
13-
{
14-
associated_table.join_foreign_type => type,
15-
associated_table.join_foreign_key => ids
16-
}
13+
query = {}
14+
query[associated_table.join_foreign_type] = type if type
15+
query[associated_table.join_foreign_key] = ids
16+
query
1717
end
1818
end
1919

@@ -23,7 +23,7 @@ def queries
2323
def type_to_ids_mapping
2424
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
2525
values.each_with_object(default_hash) do |value, hash|
26-
hash[klass(value).polymorphic_name] << convert_to_id(value)
26+
hash[klass(value)&.polymorphic_name] << convert_to_id(value)
2727
end
2828
end
2929

@@ -46,6 +46,8 @@ def convert_to_id(value)
4646
value._read_attribute(primary_key(value))
4747
when Relation
4848
value.select(primary_key(value))
49+
else
50+
value
4951
end
5052
end
5153
end

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def test_where_with_custom_primary_key
4444
assert_equal [authors(:david)], Author.where(owned_essay: essays(:david_modest_proposal))
4545
end
4646

47+
def test_where_on_polymorphic_association_with_nil
48+
assert_equal comments(:greetings), Comment.where(author: nil).first
49+
assert_equal comments(:greetings), Comment.where(author: [nil]).first
50+
end
51+
4752
def test_assigning_belongs_to_on_destroyed_object
4853
client = Client.create!(name: "Client")
4954
client.destroy!

0 commit comments

Comments
 (0)