File tree Expand file tree Collapse file tree 4 files changed +37
-0
lines changed
lib/active_record/associations Expand file tree Collapse file tree 4 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ * Clear cached ` has_one ` association after setting ` belongs_to ` association to ` nil ` .
2
+
3
+ After setting a ` belongs_to ` relation to ` nil ` and updating an unrelated attribute on the owner,
4
+ the owner should still return ` nil ` on the ` has_one ` relation.
5
+
6
+ Fixes #42597 .
7
+
8
+ * Michiel de Mare*
9
+
1
10
* OpenSSL constants are now used for Digest computations.
2
11
3
12
* Dirkjan Bussink*
Original file line number Diff line number Diff line change @@ -77,6 +77,8 @@ def replace(record)
77
77
raise_on_type_mismatch! ( record )
78
78
set_inverse_instance ( record )
79
79
@updated = true
80
+ elsif target
81
+ remove_inverse_instance ( target )
80
82
end
81
83
82
84
replace_keys ( record , force : true )
Original file line number Diff line number Diff line change @@ -1205,6 +1205,19 @@ 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_clearing_an_association_clears_the_associations_inverse
1209
+ author = Author . create ( name : "Jimmy Tolkien" )
1210
+ post = author . create_post ( title : "The silly medallion" , body : "" )
1211
+ assert_equal post , author . post
1212
+ assert_equal author , post . author
1213
+
1214
+ author . update! ( post : nil )
1215
+ assert_nil author . post
1216
+
1217
+ post . update! ( title : "The Silmarillion" )
1218
+ assert_nil author . post
1219
+ end
1220
+
1208
1221
def test_destroying_child_with_unloaded_parent_and_foreign_key_and_touch_is_possible_with_has_many_inversing
1209
1222
with_has_many_inversing do
1210
1223
book = Book . create!
Original file line number Diff line number Diff line change @@ -307,6 +307,19 @@ def test_create_association
307
307
assert_equal account , firm . reload . account
308
308
end
309
309
310
+ def test_clearing_an_association_clears_the_associations_inverse
311
+ author = Author . create ( name : "Jimmy Tolkien" )
312
+ post = author . create_post ( title : "The silly medallion" , body : "" )
313
+ assert_equal post , author . post
314
+ assert_equal author , post . author
315
+
316
+ post . update! ( author : nil )
317
+ assert_nil post . author
318
+
319
+ author . update! ( name : "J.R.R. Tolkien" )
320
+ assert_nil post . author
321
+ end
322
+
310
323
def test_create_association_with_bang
311
324
firm = Firm . create ( name : "GlobalMegaCorp" )
312
325
account = firm . create_account! ( credit_limit : 1000 )
You can’t perform that action at this time.
0 commit comments