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

fix: duplicate active record objects on inverse_of #43445

Merged
merged 1 commit into from Oct 13, 2021
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
Expand Up @@ -456,6 +456,10 @@ def replace_on_target(record, skip_callbacks, replace:, inversing: false)

yield(record) if block_given?

if !index && @replaced_or_added_targets.include?(record)
index = @target.index(record)
end

@replaced_or_added_targets << record if inversing || index || record.new_record?

if index
Expand Down
Expand Up @@ -766,6 +766,15 @@ def test_with_has_many_inversing_does_not_trigger_association_callbacks_on_set_w
end
end

def test_with_hash_many_inversing_does_not_add_duplicate_associated_objects
with_has_many_inversing(Interest) do
human = Human.new
interest = Interest.new(human: human)
human.interests << interest
assert_equal 1, human.interests.size
end
end

def test_unscope_does_not_set_inverse_when_incorrect
interest = interests(:trainspotting)
human = interest.human
Expand Down