未验证 提交 89d1d39c 编写于 作者: G Gannon McGibbon 提交者: GitHub

Merge pull request #38394 from javiyu/fix-double-object-on-inverse-creation

Fix: on accessing the parent record before creation with has_many_inv…
......@@ -278,8 +278,8 @@ def load_target
target
end
def add_to_target(record, skip_callbacks = false, &block)
if association_scope.distinct_value
def add_to_target(record, skip_callbacks: false, replace: false, &block)
if replace || association_scope.distinct_value
index = @target.index(record)
end
replace_on_target(record, index, skip_callbacks, &block)
......@@ -292,7 +292,7 @@ def target=(record)
when Array
super
else
add_to_target(record, true)
add_to_target(record, skip_callbacks: true, replace: true)
end
end
......
......@@ -509,7 +509,7 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
if target_record
existing_record = target_record
else
association.add_to_target(existing_record, :skip_callbacks)
association.add_to_target(existing_record, skip_callbacks: true)
end
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
......
......@@ -650,6 +650,16 @@ def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_man }
end
def test_building_has_many_parent_association_inverses_one_record
with_has_many_inversing do
interest = Interest.new
interest.build_man
assert_equal 1, interest.man.interests.size
interest.save!
assert_equal 1, interest.man.interests.size
end
end
end
class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册