aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/counter_cache.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/counter_cache.rb')
-rw-r--r--activerecord/lib/active_record/counter_cache.rb16
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index a5897edf03..163da8e870 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -131,6 +131,16 @@ module ActiveRecord
private
+ def _create_record(*)
+ id = super
+
+ each_counter_cached_associations do |association|
+ association.increment_counters
+ end
+
+ id
+ end
+
def destroy_row
affected_rows = super
@@ -139,5 +149,11 @@ module ActiveRecord
affected_rows
end
+ def each_counter_cached_associations
+ reflections.each do |name, reflection|
+ yield association(name) if reflection.belongs_to? && reflection.counter_cache_column
+ end
+ end
+
end
end