mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #38394 from javiyu/fix-double-object-on-inverse-creation
Fix: on accessing the parent record before creation with has_many_inv…
This commit is contained in:
commit
89d1d39c8f
3 changed files with 14 additions and 4 deletions
|
@ -278,8 +278,8 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
when Array
|
||||
super
|
||||
else
|
||||
add_to_target(record, true)
|
||||
add_to_target(record, skip_callbacks: true, replace: true)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -509,7 +509,7 @@ module ActiveRecord
|
|||
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 @@ class InverseBelongsToTests < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue