Merge pull request #48489 from gmcgibbon/_read_attribute_has_one_autosave

Use _read_attribute when autosaving has_one associations
This commit is contained in:
Eileen M. Uchitelle 2023-06-16 14:09:04 -04:00 committed by GitHub
commit 2f833a64ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -453,7 +453,7 @@ module ActiveRecord
if autosave && record.marked_for_destruction?
record.destroy
elsif autosave != false
key = reflection.options[:primary_key] ? public_send(reflection.options[:primary_key]) : id
key = reflection.options[:primary_key] ? _read_attribute(reflection.options[:primary_key].to_s) : id
if (autosave && record.changed_for_autosave?) || _record_changed?(reflection, record, key)
unless reflection.through_reflection

View File

@ -798,6 +798,13 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa
assert_includes order.books, cpk_books(:cpk_great_author_second_book)
end
def test_has_one_cpk_has_one_autosave_with_id
book = Cpk::Book.create!(author_id: 1, number: 3, shop_id: 2)
order = Cpk::OrderWithPrimaryKeyAssociatedBook.create!(book: book, shop_id: 2)
assert_equal(book.order.id, order.id)
end
def test_assign_ids_for_through_a_belongs_to
firm = Firm.new("name" => "Apple")
firm.developer_ids = [developers(:david).id, developers(:jamis).id]

View File

@ -16,4 +16,8 @@ module Cpk
has_many :books
has_one :book
end
class OrderWithPrimaryKeyAssociatedBook < Order
has_one :book, primary_key: :id, foreign_key: :order_id
end
end