Skip to content

Commit

Permalink
Merge pull request #48491 from adrianna-chang-shopify/ac-fk-changed-f…
Browse files Browse the repository at this point in the history
…or-cpk

Bugfix: Ensure has_one associations saved when part of CPK has changed
  • Loading branch information
eileencodes committed Jun 16, 2023
2 parents 5154520 + 4b6fb72 commit 3f24fa8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -482,7 +482,10 @@ def _record_changed?(reflection, record, key)
def association_foreign_key_changed?(reflection, record, key)
return false if reflection.through_reflection?

record._has_attribute?(reflection.foreign_key) && record._read_attribute(reflection.foreign_key) != key
foreign_key = Array(reflection.foreign_key)
return false unless foreign_key.all? { |key| record._has_attribute?(key) }

foreign_key.map { |key| record._read_attribute(key) } != Array(key)
end

def inverse_polymorphic_association_changed?(reflection, record)
Expand Down
Expand Up @@ -376,6 +376,16 @@ def test_belongs_to_with_inverse_association_for_composite_primary_key
assert_equal order_id, book.order_id
end

def test_should_set_composite_foreign_key_on_association_when_key_changes_on_associated_record
book = Cpk::Book.create!(id: [1, 2], title: "The Well-Grounded Rubyist")
order = Cpk::Order.create!(id: [1, 2], book: book)

order.shop_id = 3
order.save!

assert_equal 3, book.shop_id
end

def test_building_the_belonging_object_with_implicit_sti_base_class
account = Account.new
company = account.build_firm
Expand Down

0 comments on commit 3f24fa8

Please sign in to comment.