mirror of https://github.com/rails/rails
Merge pull request #48489 from gmcgibbon/_read_attribute_has_one_autosave
Use _read_attribute when autosaving has_one associations
This commit is contained in:
commit
2f833a64ae
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue