mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #40951 from kamipo/fix_where_with_polymorphic_association
Fix `where` on polymorphic association with non Active Record object
This commit is contained in:
commit
056c52a2b6
3 changed files with 14 additions and 10 deletions
|
@ -93,11 +93,8 @@ module ActiveRecord
|
||||||
# PriceEstimate.where(estimate_of: treasure)
|
# PriceEstimate.where(estimate_of: treasure)
|
||||||
associated_table = table.associated_table(key)
|
associated_table = table.associated_table(key)
|
||||||
if associated_table.polymorphic_association?
|
if associated_table.polymorphic_association?
|
||||||
case value.is_a?(Array) ? value.first : value
|
value = [value] unless value.is_a?(Array)
|
||||||
when Base, Relation
|
klass = PolymorphicArrayValue
|
||||||
value = [value] unless value.is_a?(Array)
|
|
||||||
klass = PolymorphicArrayValue
|
|
||||||
end
|
|
||||||
elsif associated_table.through_association?
|
elsif associated_table.through_association?
|
||||||
next associated_table.predicate_builder.expand_from_hash(
|
next associated_table.predicate_builder.expand_from_hash(
|
||||||
associated_table.primary_key => value
|
associated_table.primary_key => value
|
||||||
|
|
|
@ -10,10 +10,10 @@ module ActiveRecord
|
||||||
|
|
||||||
def queries
|
def queries
|
||||||
type_to_ids_mapping.map do |type, ids|
|
type_to_ids_mapping.map do |type, ids|
|
||||||
{
|
query = {}
|
||||||
associated_table.join_foreign_type => type,
|
query[associated_table.join_foreign_type] = type if type
|
||||||
associated_table.join_foreign_key => ids
|
query[associated_table.join_foreign_key] = ids
|
||||||
}
|
query
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ module ActiveRecord
|
||||||
def type_to_ids_mapping
|
def type_to_ids_mapping
|
||||||
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
||||||
values.each_with_object(default_hash) do |value, hash|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ module ActiveRecord
|
||||||
value._read_attribute(primary_key(value))
|
value._read_attribute(primary_key(value))
|
||||||
when Relation
|
when Relation
|
||||||
value.select(primary_key(value))
|
value.select(primary_key(value))
|
||||||
|
else
|
||||||
|
value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,6 +44,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal [authors(:david)], Author.where(owned_essay: essays(:david_modest_proposal))
|
assert_equal [authors(:david)], Author.where(owned_essay: essays(:david_modest_proposal))
|
||||||
end
|
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
|
def test_assigning_belongs_to_on_destroyed_object
|
||||||
client = Client.create!(name: "Client")
|
client = Client.create!(name: "Client")
|
||||||
client.destroy!
|
client.destroy!
|
||||||
|
|
Loading…
Reference in a new issue