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:
commit
317547e0e7
2 changed files with 16 additions and 0 deletions
|
@ -276,6 +276,8 @@ module ActiveRecord
|
|||
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
|
||||
|
|
|
@ -1205,6 +1205,20 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
|||
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: "")
|
||||
|
|
Loading…
Reference in a new issue