Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Error for Array-Based Composite Keys in nested_attribute #613

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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