Skip to content

Commit c2af8db

Browse files
authored
Merge pull request rails#52186 from jackozi/missing-on-cpk
Fix `missing(:association)` and `associated(:association)` for composite primary keys
2 parents b291408 + c159cb6 commit c2af8db

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ def associated(*associations)
9292
@scope.joins!(association)
9393
end
9494

95+
association_conditions = Array(reflection.association_primary_key).index_with(nil)
9596
if reflection.options[:class_name]
96-
self.not(association => { reflection.association_primary_key => nil })
97+
self.not(association => association_conditions)
9798
else
98-
self.not(reflection.table_name => { reflection.association_primary_key => nil })
99+
self.not(reflection.table_name => association_conditions)
99100
end
100101
end
101102

@@ -124,10 +125,11 @@ def missing(*associations)
124125
associations.each do |association|
125126
reflection = scope_association_reflection(association)
126127
@scope.left_outer_joins!(association)
128+
association_conditions = Array(reflection.association_primary_key).index_with(nil)
127129
if reflection.options[:class_name]
128-
@scope.where!(association => { reflection.association_primary_key => nil })
130+
@scope.where!(association => association_conditions)
129131
else
130-
@scope.where!(reflection.table_name => { reflection.association_primary_key => nil })
132+
@scope.where!(reflection.table_name => association_conditions)
131133
end
132134
end
133135

activerecord/test/cases/relation/where_chain_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require "models/comment"
99
require "models/categorization"
1010
require "models/book"
11+
require "models/cpk"
1112

1213
module ActiveRecord
1314
class WhereChainTest < ActiveRecord::TestCase
@@ -113,6 +114,13 @@ def test_associated_with_add_left_outer_joins_before
113114
end
114115
end
115116

117+
def test_associated_with_composite_primary_key
118+
author = Cpk::Author.create!(id: [1, 2])
119+
Cpk::Book.create!(id: [author.id, 2])
120+
121+
assert_predicate Cpk::Author.where.associated(:books), :any?
122+
end
123+
116124
def test_missing_with_association
117125
assert_predicate posts(:authorless).author, :blank?
118126
assert_equal [posts(:authorless)], Post.where.missing(:author).to_a
@@ -188,6 +196,12 @@ def test_missing_with_enum_extended_late
188196
assert_equal Author.find(2), Author.order(id: :desc).joins(:reading_listing).where.missing(:unread_listing).extending(Author::NamedExtension).first
189197
end
190198

199+
def test_missing_with_composite_primary_key
200+
Cpk::Book.create!(id: [1, 2])
201+
202+
assert_predicate Cpk::Book.where.missing(:author), :any?
203+
end
204+
191205
def test_not_inverts_where_clause
192206
relation = Post.where.not(title: "hello")
193207
expected_where_clause = Post.where(title: "hello").where_clause.invert

0 commit comments

Comments
 (0)