File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -92,10 +92,11 @@ def associated(*associations)
92
92
@scope . joins! ( association )
93
93
end
94
94
95
+ association_conditions = Array ( reflection . association_primary_key ) . index_with ( nil )
95
96
if reflection . options [ :class_name ]
96
- self . not ( association => { reflection . association_primary_key => nil } )
97
+ self . not ( association => association_conditions )
97
98
else
98
- self . not ( reflection . table_name => { reflection . association_primary_key => nil } )
99
+ self . not ( reflection . table_name => association_conditions )
99
100
end
100
101
end
101
102
@@ -124,10 +125,11 @@ def missing(*associations)
124
125
associations . each do |association |
125
126
reflection = scope_association_reflection ( association )
126
127
@scope . left_outer_joins! ( association )
128
+ association_conditions = Array ( reflection . association_primary_key ) . index_with ( nil )
127
129
if reflection . options [ :class_name ]
128
- @scope . where! ( association => { reflection . association_primary_key => nil } )
130
+ @scope . where! ( association => association_conditions )
129
131
else
130
- @scope . where! ( reflection . table_name => { reflection . association_primary_key => nil } )
132
+ @scope . where! ( reflection . table_name => association_conditions )
131
133
end
132
134
end
133
135
Original file line number Diff line number Diff line change 8
8
require "models/comment"
9
9
require "models/categorization"
10
10
require "models/book"
11
+ require "models/cpk"
11
12
12
13
module ActiveRecord
13
14
class WhereChainTest < ActiveRecord ::TestCase
@@ -113,6 +114,13 @@ def test_associated_with_add_left_outer_joins_before
113
114
end
114
115
end
115
116
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
+
116
124
def test_missing_with_association
117
125
assert_predicate posts ( :authorless ) . author , :blank?
118
126
assert_equal [ posts ( :authorless ) ] , Post . where . missing ( :author ) . to_a
@@ -188,6 +196,12 @@ def test_missing_with_enum_extended_late
188
196
assert_equal Author . find ( 2 ) , Author . order ( id : :desc ) . joins ( :reading_listing ) . where . missing ( :unread_listing ) . extending ( Author ::NamedExtension ) . first
189
197
end
190
198
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
+
191
205
def test_not_inverts_where_clause
192
206
relation = Post . where . not ( title : "hello" )
193
207
expected_where_clause = Post . where ( title : "hello" ) . where_clause . invert
You can’t perform that action at this time.
0 commit comments