1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #42729 from Shopify/fix-has-many-inversing-remove

Fix clearing the inverse relation when has_many_inversing is enabled
This commit is contained in:
Jean Boussier 2021-07-08 12:18:14 +02:00 committed by GitHub
commit 317547e0e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -276,6 +276,8 @@ module ActiveRecord
return super unless reflection.klass.has_many_inversing return super unless reflection.klass.has_many_inversing
case record case record
when nil
# It's not possible to remove the record from the inverse association.
when Array when Array
super super
else else

View file

@ -1205,6 +1205,20 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal companies(:another_firm), client.firm_with_condition assert_equal companies(:another_firm), client.firm_with_condition
end 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 def test_clearing_an_association_clears_the_associations_inverse
author = Author.create(name: "Jimmy Tolkien") author = Author.create(name: "Jimmy Tolkien")
post = author.create_post(title: "The silly medallion", body: "") post = author.create_post(title: "The silly medallion", body: "")