Skip to content

Commit ccb6657

Browse files
authored
Merge pull request rails#42601 from mdemare/has_one_cache
Clear cached has_one association after removing belongs_to association
2 parents 30033e6 + 484303d commit ccb6657

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
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+
110
* OpenSSL constants are now used for Digest computations.
211

312
*Dirkjan Bussink*

activerecord/lib/active_record/associations/belongs_to_association.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def replace(record)
7777
raise_on_type_mismatch!(record)
7878
set_inverse_instance(record)
7979
@updated = true
80+
elsif target
81+
remove_inverse_instance(target)
8082
end
8183

8284
replace_keys(record, force: true)

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,19 @@ def test_reassigning_the_parent_id_updates_the_object
12051205
assert_equal companies(:another_firm), client.firm_with_condition
12061206
end
12071207

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+
12081221
def test_destroying_child_with_unloaded_parent_and_foreign_key_and_touch_is_possible_with_has_many_inversing
12091222
with_has_many_inversing do
12101223
book = Book.create!

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ def test_create_association
307307
assert_equal account, firm.reload.account
308308
end
309309

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+
310323
def test_create_association_with_bang
311324
firm = Firm.create(name: "GlobalMegaCorp")
312325
account = firm.create_account!(credit_limit: 1000)

0 commit comments

Comments
 (0)