Skip to content

Commit ed35eaa

Browse files
authored
Merge pull request rails#53706 from fatkodima/fix-where-with-cpk-nil-association
Fix finding by nil composite primary key association
2 parents b7165a3 + 2481576 commit ed35eaa

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def polymorphic_clause?
5959
def convert_to_id(value)
6060
if primary_key.is_a?(Array)
6161
primary_key.map do |attribute|
62+
next nil if value.nil?
63+
6264
if attribute == "id"
6365
value.id_value
6466
else

activerecord/test/cases/relation/where_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ def test_with_tuple_syntax_and_large_values_list
149149
end
150150
end
151151

152+
def test_where_with_nil_cpk_association
153+
order = Cpk::Order.create!(id: [1, 2])
154+
book = order.books.create!(id: [3, 4])
155+
assert_includes Cpk::Book.where(order: order), book
156+
157+
book.update!(order: nil)
158+
assert_includes Cpk::Book.where(order: nil), book
159+
end
160+
152161
def test_belongs_to_shallow_where
153162
author = Author.new
154163
author.id = 1

0 commit comments

Comments
 (0)