File tree Expand file tree Collapse file tree 3 files changed +31
-16
lines changed
activerecord/lib/active_record Expand file tree Collapse file tree 3 files changed +31
-16
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,14 @@ def updated?
31
31
@updated
32
32
end
33
33
34
+ def decrement_counters
35
+ with_cache_name { |name | decrement_counter name }
36
+ end
37
+
38
+ def increment_counters
39
+ with_cache_name { |name | increment_counter name }
40
+ end
41
+
34
42
private
35
43
36
44
def find_target?
@@ -51,13 +59,15 @@ def update_counters(record)
51
59
end
52
60
end
53
61
54
- def decrement_counters
55
- with_cache_name { |name | decrement_counter name }
62
+ def decrement_counter ( counter_cache_name )
63
+ if foreign_key_present?
64
+ klass . decrement_counter ( counter_cache_name , target_id )
65
+ end
56
66
end
57
67
58
- def decrement_counter counter_cache_name
68
+ def increment_counter ( counter_cache_name )
59
69
if foreign_key_present?
60
- klass . decrement_counter ( counter_cache_name , target_id )
70
+ klass . increment_counter ( counter_cache_name , target_id )
61
71
end
62
72
end
63
73
Original file line number Diff line number Diff line change @@ -26,16 +26,9 @@ def self.define_accessors(mixin, reflection)
26
26
private
27
27
28
28
def self . add_counter_cache_methods ( mixin )
29
- return if mixin . method_defined? :belongs_to_counter_cache_after_create
29
+ return if mixin . method_defined? :belongs_to_counter_cache_after_update
30
30
31
31
mixin . class_eval do
32
- def belongs_to_counter_cache_after_create ( reflection )
33
- if record = send ( reflection . name )
34
- cache_column = reflection . counter_cache_column
35
- record . class . increment_counter ( cache_column , record . id )
36
- @_after_create_counter_called = true
37
- end
38
- end
39
32
40
33
def belongs_to_counter_cache_after_destroy ( reflection )
41
34
foreign_key = reflection . foreign_key . to_sym
@@ -74,10 +67,6 @@ def belongs_to_counter_cache_after_update(reflection)
74
67
def self . add_counter_cache_callbacks ( model , reflection )
75
68
cache_column = reflection . counter_cache_column
76
69
77
- model . after_create lambda { |record |
78
- record . belongs_to_counter_cache_after_create ( reflection )
79
- }
80
-
81
70
model . after_destroy lambda { |record |
82
71
record . belongs_to_counter_cache_after_destroy ( reflection )
83
72
}
Original file line number Diff line number Diff line change @@ -131,6 +131,16 @@ def clear_destroy_state
131
131
132
132
private
133
133
134
+ def _create_record ( *)
135
+ id = super
136
+
137
+ each_counter_cached_associations do |association |
138
+ association . increment_counters
139
+ end
140
+
141
+ id
142
+ end
143
+
134
144
def destroy_row
135
145
affected_rows = super
136
146
@@ -139,5 +149,11 @@ def destroy_row
139
149
affected_rows
140
150
end
141
151
152
+ def each_counter_cached_associations
153
+ reflections . each do |name , reflection |
154
+ yield association ( name ) if reflection . belongs_to? && reflection . counter_cache_column
155
+ end
156
+ end
157
+
142
158
end
143
159
end
You can’t perform that action at this time.
0 commit comments