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

Bugfix: Ensure has_one associations saved when part of CPK has changed #48491

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/autosave_association.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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