File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed
lib/active_record/associations Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -276,6 +276,8 @@ def target=(record)
276
276
return super unless reflection . klass . has_many_inversing
277
277
278
278
case record
279
+ when nil
280
+ # It's not possible to remove the record from the inverse association.
279
281
when Array
280
282
super
281
283
else
Original file line number Diff line number Diff line change @@ -1205,6 +1205,20 @@ def test_reassigning_the_parent_id_updates_the_object
1205
1205
assert_equal companies ( :another_firm ) , client . firm_with_condition
1206
1206
end
1207
1207
1208
+ def test_assigning_nil_on_an_association_clears_the_associations_inverse
1209
+ with_has_many_inversing do
1210
+ book = Book . create!
1211
+ citation = book . citations . create!
1212
+
1213
+ assert_same book , citation . book
1214
+
1215
+ assert_nothing_raised do
1216
+ citation . book = nil
1217
+ citation . save!
1218
+ end
1219
+ end
1220
+ end
1221
+
1208
1222
def test_clearing_an_association_clears_the_associations_inverse
1209
1223
author = Author . create ( name : "Jimmy Tolkien" )
1210
1224
post = author . create_post ( title : "The silly medallion" , body : "" )
You can’t perform that action at this time.
0 commit comments