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 clearing the inverse relation when has_many_inversing is enabled #42729

Merged
merged 1 commit into from Jul 8, 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 @@ -276,6 +276,8 @@ def target=(record)
return super unless reflection.klass.has_many_inversing

case record
when nil
# It's not possible to remove the record from the inverse association.
when Array
super
else
Expand Down
Expand Up @@ -1205,6 +1205,20 @@ def test_reassigning_the_parent_id_updates_the_object
assert_equal companies(:another_firm), client.firm_with_condition
end

def test_assigning_nil_on_an_association_clears_the_associations_inverse
with_has_many_inversing do
book = Book.create!
citation = book.citations.create!

assert_same book, citation.book

assert_nothing_raised do
citation.book = nil
citation.save!
end
end
end

def test_clearing_an_association_clears_the_associations_inverse
author = Author.create(name: "Jimmy Tolkien")
post = author.create_post(title: "The silly medallion", body: "")
Expand Down