Skip to content

Commit 109d1b2

Browse files
committed
Use inheritance chain instead of callbacks to increment counter caches after create
1 parent a1e2db2 commit 109d1b2

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

activerecord/lib/active_record/associations/belongs_to_association.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ def updated?
3131
@updated
3232
end
3333

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+
3442
private
3543

3644
def find_target?
@@ -51,13 +59,15 @@ def update_counters(record)
5159
end
5260
end
5361

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
5666
end
5767

58-
def decrement_counter counter_cache_name
68+
def increment_counter(counter_cache_name)
5969
if foreign_key_present?
60-
klass.decrement_counter(counter_cache_name, target_id)
70+
klass.increment_counter(counter_cache_name, target_id)
6171
end
6272
end
6373

activerecord/lib/active_record/associations/builder/belongs_to.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@ def self.define_accessors(mixin, reflection)
2626
private
2727

2828
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
3030

3131
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
3932

4033
def belongs_to_counter_cache_after_destroy(reflection)
4134
foreign_key = reflection.foreign_key.to_sym
@@ -74,10 +67,6 @@ def belongs_to_counter_cache_after_update(reflection)
7467
def self.add_counter_cache_callbacks(model, reflection)
7568
cache_column = reflection.counter_cache_column
7669

77-
model.after_create lambda { |record|
78-
record.belongs_to_counter_cache_after_create(reflection)
79-
}
80-
8170
model.after_destroy lambda { |record|
8271
record.belongs_to_counter_cache_after_destroy(reflection)
8372
}

activerecord/lib/active_record/counter_cache.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ def clear_destroy_state
131131

132132
private
133133

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+
134144
def destroy_row
135145
affected_rows = super
136146

@@ -139,5 +149,11 @@ def destroy_row
139149
affected_rows
140150
end
141151

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+
142158
end
143159
end

0 commit comments

Comments
 (0)