Fix where on polymorphic association with non Active Record object

It is a regression for #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 #40937.
This commit is contained in:
Ryuta Kamizono 2020-12-28 06:35:23 +09:00
parent f3071970c2
commit bc9beb27aa
3 changed files with 14 additions and 10 deletions

@ -93,11 +93,8 @@ def expand_from_hash(attributes, &block)
# PriceEstimate.where(estimate_of: treasure)
associated_table = table.associated_table(key)
if associated_table.polymorphic_association?
case value.is_a?(Array) ? value.first : value
when Base, Relation
value = [value] unless value.is_a?(Array)
klass = PolymorphicArrayValue
end
value = [value] unless value.is_a?(Array)
klass = PolymorphicArrayValue
elsif associated_table.through_association?
next associated_table.predicate_builder.expand_from_hash(
associated_table.primary_key => value

@ -10,10 +10,10 @@ def initialize(associated_table, values)
def queries
type_to_ids_mapping.map do |type, ids|
{
associated_table.join_foreign_type => type,
associated_table.join_foreign_key => ids
}
query = {}
query[associated_table.join_foreign_type] = type if type
query[associated_table.join_foreign_key] = ids
query
end
end
@ -23,7 +23,7 @@ def queries
def type_to_ids_mapping
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
values.each_with_object(default_hash) do |value, hash|
hash[klass(value).polymorphic_name] << convert_to_id(value)
hash[klass(value)&.polymorphic_name] << convert_to_id(value)
end
end
@ -46,6 +46,8 @@ def convert_to_id(value)
value._read_attribute(primary_key(value))
when Relation
value.select(primary_key(value))
else
value
end
end
end

@ -44,6 +44,11 @@ def test_where_with_custom_primary_key
assert_equal [authors(:david)], Author.where(owned_essay: essays(:david_modest_proposal))
end
def test_where_on_polymorphic_association_with_nil
assert_equal comments(:greetings), Comment.where(author: nil).first
assert_equal comments(:greetings), Comment.where(author: [nil]).first
end
def test_assigning_belongs_to_on_destroyed_object
client = Client.create!(name: "Client")
client.destroy!