Skip to content

Commit

Permalink
Merge pull request #613 from yebityon/fix/nested-attriburte
Browse files Browse the repository at this point in the history
Fix Error for Array-Based Composite Keys in nested_attribute
  • Loading branch information
cfis committed Dec 4, 2023
2 parents e21e668 + f9a3f80 commit 375e327
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/composite_primary_keys/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
unless reject_new_record?(association_name, attributes)
association.reader.build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes["id"].to_s }
elsif existing_record = cpk_detect_record(attributes['id'], existing_records)
unless call_reject_if(association_name, attributes)
# Make sure we are operating on the actual object which is in the association's
# proxy_target array (either by finding it, or adding it if not found)
# Take into account that the proxy_target may have changed due to callbacks
target_record = association.target.detect { |record| record.id.to_s == attributes["id"].to_s }
target_record = cpk_detect_record(attributes['id'], association.target)
if target_record
existing_record = target_record
else
Expand Down
23 changes: 23 additions & 0 deletions test/test_nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,27 @@ def test_nested_atttribute_update_3
assert_equal(reference_code.code_label, 'XX')
assert_equal(reference_code.abbreviation, 'Xx')
end

def test_nested_atttribute_update_4
code_id = 1003

reference_type = reference_types(:name_prefix)
reference_type.update :reference_codes_attributes => [{
:reference_code => code_id,
:code_label => 'XX',
:abbreviation => 'Xx'
}]
assert_not_nil ReferenceCode.find_by_reference_code(code_id)
reference_code = ReferenceCode.find_by_reference_code(code_id)
# directly pass :id as a array
reference_type.update :reference_codes_attributes => [{
:id => [reference_type.reference_type_id, code_id],
:code_label => 'AAA',
:abbreviation => 'Aaa'
}]

reference_code = ReferenceCode.find_by_reference_code(code_id)
assert_kind_of(ReferenceCode, reference_code)
assert_equal(reference_code.code_label, 'AAA')
end
end

0 comments on commit 375e327

Please sign in to comment.