mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix clearing the inverse relation when has_many_inversing is enabled
https://github.com/rails/rails/pull/42601 fixed clearing the inverse relation, but it didn't account for collection associations. For these, just assigning `nil` isn't possible because we need the record to remove it from the collection. So this PR introduce an explicit method for this purpose rather than reuse `inversed_from(nil)`.
This commit is contained in:
parent
7373b5819a
commit
6d7235dd20
2 changed files with 16 additions and 0 deletions
|
@ -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
|
||||||
|
|
|
@ -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: "")
|
||||||
|
|
Loading…
Reference in a new issue