Merge pull request #48690 from adrianna-chang-shopify/ac-merge-target-list-cpk-fix

Prevent `#merge_target_lists` from clearing id column on composite primary key records
This commit is contained in:
Eileen M. Uchitelle 2023-07-10 09:06:07 -04:00 committed by GitHub
commit 56c784d808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

@ -333,7 +333,11 @@ def merge_target_lists(persisted, memory)
if mem_record = memory.delete(record)
((record.attribute_names & mem_record.attribute_names) - mem_record.changed_attribute_names_to_save - mem_record.class._attr_readonly).each do |name|
mem_record._write_attribute(name, record[name])
if name == "id" && mem_record.class.composite_primary_key?
mem_record.class.primary_key.zip(record[name]) { |attr, value| mem_record._write_attribute(attr, value) }
else
mem_record._write_attribute(name, record[name])
end
end
mem_record

@ -79,6 +79,16 @@ def test_loading_the_association_target_should_load_most_recent_attributes_for_c
assert_equal "Deck", ship.parts[0].name
end
def test_loading_cpk_association_when_persisted_and_in_memory_differ
order = Cpk::Order.create!(id: [1, 2], status: "paid")
book = order.books.create!(id: [3, 4], title: "Book")
Cpk::Book.find(book.id).update_columns(title: "A different title")
order.books.load
assert_equal [3, 4], book.id
end
def test_include_with_order_works
assert_nothing_raised { Account.all.merge!(order: "id", includes: :firm).first }
assert_nothing_raised { Account.all.merge!(order: :id, includes: :firm).first }