instance method
reset_counters
Ruby on Rails 8.1.2
Since v4.0.13Signature
reset_counters(id, *counters, touch: nil)
Resets one or more counter caches to their correct value using an SQL count query. This is useful when adding new counter caches, or if the counter has been corrupted or modified directly by SQL.
Parameters
-
id- The id of the object you wish to reset a counter on or an array of ids. -
counters- One or more association counters to reset. Association name or counter name can be given. -
:touch- Touch timestamp columns when updating. Passtrueto touchupdated_atand/orupdated_on. Pass a symbol to touch that column or an array of symbols to touch just those ones.
Examples
# For the Post with id #1, reset the comments_count Post.reset_counters(1, :comments) # For posts with ids #1 and #2, reset the comments_count Post.reset_counters([1, 2], :comments) # Like above, but also touch the updated_at and/or updated_on # attributes. Post.reset_counters(1, :comments, touch: true)
Parameters
-
idreq -
countersrest -
touchkey = nil
Source
# File activerecord/lib/active_record/counter_cache.rb, line 37
def reset_counters(id, *counters, touch: nil)
ids = if composite_primary_key?
if id.first.is_a?(Array)
id
else
[id]
end
else
Array(id)
end
updates = Hash.new { |h, k| h[k] = {} }
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
counter_association = counter_association.to_sym
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
counts =
unscoped
.joins(counter_association)
.where(primary_key => ids)
.group(primary_key)
.count(:all)
ids.each do |id|
updates[id].merge!(counter_name => counts[id] || 0)
end
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.each_value do |record_updates|
record_updates.merge!(touch_updates)
end
end
updates.each do |id, record_updates|
unscoped.where(primary_key => [id]).update_all(record_updates)
end
true
end
Defined in activerecord/lib/active_record/counter_cache.rb line 37
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::CounterCache::ClassMethods