File tree Expand file tree Collapse file tree 2 files changed +16
-7
lines changed
lib/active_record/associations Expand file tree Collapse file tree 2 files changed +16
-7
lines changed Original file line number Diff line number Diff line change @@ -256,14 +256,16 @@ def replace(other_array)
256
256
end
257
257
258
258
def include? ( record )
259
- if record . is_a? ( reflection . klass )
260
- if record . new_record?
261
- include_in_memory? ( record )
262
- else
263
- loaded? ? target . include? ( record ) : scope . exists? ( record . id )
264
- end
259
+ klass = reflection . klass
260
+ return false unless record . is_a? ( klass )
261
+
262
+ if record . new_record?
263
+ include_in_memory? ( record )
264
+ elsif loaded?
265
+ target . include? ( record )
265
266
else
266
- false
267
+ record_id = klass . composite_primary_key? ? klass . primary_key . zip ( record . id ) . to_h : record . id
268
+ scope . exists? ( record_id )
267
269
end
268
270
end
269
271
Original file line number Diff line number Diff line change @@ -2029,6 +2029,13 @@ def test_included_in_collection
2029
2029
assert_equal true , companies ( :first_firm ) . clients . include? ( Client . find ( 2 ) )
2030
2030
end
2031
2031
2032
+ def test_included_in_collection_for_composite_keys
2033
+ great_author = cpk_authors ( :cpk_great_author )
2034
+ book = great_author . books . first
2035
+
2036
+ assert great_author . books . include? ( book )
2037
+ end
2038
+
2032
2039
def test_included_in_collection_for_new_records
2033
2040
client = Client . create ( name : "Persisted" )
2034
2041
assert_nil client . client_of
You can’t perform that action at this time.
0 commit comments