module ActiveRecord::CounterCache::ClassMethods
def reset_counters(id, *counters, touch: nil)
# attributes.
# Like above, but also touch the updated_at and/or updated_on
Post.reset_counters(1, :comments)
# For the Post with id #1, reset the comments_count
==== Examples
touch that column or an array of symbols to touch just those ones.
Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
* :touch - Touch timestamp columns when updating.
* +counters+ - One or more association counters to reset. Association name or counter name can be given.
* +id+ - The id of the object you wish to reset a counter on.
==== Parameters
counter has been corrupted or modified directly by SQL.
count query. This is useful when adding new counter caches, or if the
Resets one or more counter caches to their correct value using an SQL
def reset_counters(id, *counters, touch: nil) object = find(id) updates = {} counters.each do |counter_association| has_many_association = _reflect_on_association(counter_association) unless has_many_association has_many = reflect_on_all_associations(:has_many) has_many_association = has_many.find { |association| association.counter_cache_column && association.counter_cache_column.to_sym == counter_association.to_sym } counter_association = has_many_association.plural_name if has_many_association end raise ArgumentError, "'#{name}' has no association called '#{counter_association}'" unless has_many_association if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection has_many_association = has_many_association.through_reflection end foreign_key = has_many_association.foreign_key.to_s child_class = has_many_association.klass reflection = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? } counter_name = reflection.counter_cache_column count_was = object.send(counter_name) count = object.send(counter_association).count(:all) updates[counter_name] = count if count != count_was end if touch names = touch if touch != true names = Array.wrap(names) options = names.extract_options! touch_updates = touch_attributes_with_time(*names, **options) updates.merge!(touch_updates) end unscoped.where(primary_key => [object.id]).update_all(updates) if updates.any? true end