@@ -524,12 +524,12 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
524
524
unless reject_new_record? ( association_name , attributes )
525
525
association . reader . build ( attributes . except ( *UNASSIGNABLE_KEYS ) )
526
526
end
527
- elsif existing_record = find_record_by_id ( existing_records , attributes [ "id" ] )
527
+ elsif existing_record = find_record_by_id ( association . klass , existing_records , attributes [ "id" ] )
528
528
unless call_reject_if ( association_name , attributes )
529
529
# Make sure we are operating on the actual object which is in the association's
530
530
# proxy_target array (either by finding it, or adding it if not found)
531
531
# Take into account that the proxy_target may have changed due to callbacks
532
- target_record = find_record_by_id ( association . target , attributes [ "id" ] )
532
+ target_record = find_record_by_id ( association . klass , association . target , attributes [ "id" ] )
533
533
if target_record
534
534
existing_record = target_record
535
535
else
@@ -621,10 +621,8 @@ def raise_nested_attributes_record_not_found!(association_name, record_id)
621
621
model , "id" , record_id )
622
622
end
623
623
624
- def find_record_by_id ( records , id )
625
- return if records . empty?
626
-
627
- if records . first . class . composite_primary_key?
624
+ def find_record_by_id ( klass , records , id )
625
+ if klass . composite_primary_key?
628
626
id = Array ( id ) . map ( &:to_s )
629
627
records . find { |record | Array ( record . id ) . map ( &:to_s ) == id }
630
628
else
0 commit comments