File tree Expand file tree Collapse file tree 4 files changed +35
-11
lines changed Expand file tree Collapse file tree 4 files changed +35
-11
lines changed Original file line number Diff line number Diff line change @@ -52,7 +52,6 @@ def reset
52
52
@loaded = false
53
53
@target = nil
54
54
@stale_state = nil
55
- @inversed = false
56
55
end
57
56
58
57
def reset_negative_cache # :nodoc:
@@ -78,7 +77,6 @@ def loaded?
78
77
def loaded!
79
78
@loaded = true
80
79
@stale_state = stale_state
81
- @inversed = false
82
80
end
83
81
84
82
# The target is stale if the target no longer points to the record(s) that the
@@ -88,7 +86,7 @@ def loaded!
88
86
#
89
87
# Note that if the target has not been loaded, it is not considered stale.
90
88
def stale_target?
91
- ! @inversed && loaded? && @stale_state != stale_state
89
+ loaded? && @stale_state != stale_state
92
90
end
93
91
94
92
# Sets the target of this association to <tt>\target</tt>, and the \loaded flag to +true+.
@@ -137,15 +135,11 @@ def remove_inverse_instance(record)
137
135
138
136
def inversed_from ( record )
139
137
self . target = record
140
- @inversed = !!record
141
138
end
142
139
143
140
def inversed_from_queries ( record )
144
141
if inversable? ( record )
145
142
self . target = record
146
- @inversed = true
147
- else
148
- @inversed = false
149
143
end
150
144
end
151
145
Original file line number Diff line number Diff line change @@ -404,6 +404,8 @@ def save_collection_association(reflection)
404
404
saved = true
405
405
406
406
if autosave != false && ( new_record_before_save || record . new_record? )
407
+ association . set_inverse_instance ( record )
408
+
407
409
if autosave
408
410
saved = association . insert_record ( record , false )
409
411
elsif !reflection . nested?
@@ -447,9 +449,7 @@ def save_has_one_association(reflection)
447
449
if ( autosave && record . changed_for_autosave? ) || record_changed? ( reflection , record , key )
448
450
unless reflection . through_reflection
449
451
record [ reflection . foreign_key ] = key
450
- if inverse_reflection = reflection . inverse_of
451
- record . association ( inverse_reflection . name ) . inversed_from ( self )
452
- end
452
+ association . set_inverse_instance ( record )
453
453
end
454
454
455
455
saved = record . save ( validate : !autosave )
Original file line number Diff line number Diff line change @@ -1440,7 +1440,7 @@ def test_calling_update_on_id_changes_the_counter_cache
1440
1440
assert_equal original_count , topic . reload . replies_count
1441
1441
end
1442
1442
1443
- def test_calling_update_changing_ids_doesnt_change_counter_cache
1443
+ def test_calling_update_changing_ids_changes_the_counter_cache
1444
1444
topic1 = Topic . find ( 1 )
1445
1445
topic2 = Topic . find ( 3 )
1446
1446
original_count1 = topic1 . replies . to_a . size
@@ -1458,6 +1458,24 @@ def test_calling_update_changing_ids_doesnt_change_counter_cache
1458
1458
assert_equal original_count2 , topic2 . reload . replies_count
1459
1459
end
1460
1460
1461
+ def test_calling_update_changing_ids_of_inversed_association_changes_the_counter_cache
1462
+ assert_predicate Post . reflect_on_association ( :comments ) , :has_inverse?
1463
+
1464
+ post1 = Post . first
1465
+ post2 = Post . second
1466
+
1467
+ original_count1 = post1 . comments . count
1468
+ original_count2 = post2 . comments . count
1469
+
1470
+ post1 . comments . first . update ( post_id : post2 . id )
1471
+ assert_equal original_count1 - 1 , post1 . reload . comments_count
1472
+ assert_equal original_count2 + 1 , post2 . reload . comments_count
1473
+
1474
+ post2 . comments . first . update ( post_id : post1 . id )
1475
+ assert_equal original_count1 , post1 . reload . comments_count
1476
+ assert_equal original_count2 , post2 . reload . comments_count
1477
+ end
1478
+
1461
1479
def test_deleting_a_collection
1462
1480
force_signal37_to_load_all_clients_of_firm
1463
1481
Original file line number Diff line number Diff line change @@ -664,6 +664,18 @@ def test_inverse_works_when_the_association_self_references_the_same_object
664
664
comment . body = "OMG"
665
665
assert_equal comment . body , comment . children . first . parent . body
666
666
end
667
+
668
+ def test_changing_the_association_id_makes_the_inversed_association_target_stale
669
+ post1 = Post . first
670
+ post2 = Post . second
671
+ comment = post1 . comments . first
672
+
673
+ assert_same post1 , comment . post
674
+
675
+ comment . update! ( post_id : post2 . id )
676
+
677
+ assert_equal post2 , comment . post
678
+ end
667
679
end
668
680
669
681
class InverseBelongsToTests < ActiveRecord ::TestCase
You can’t perform that action at this time.
0 commit comments