module ActiveRecord::CounterCache::ClassMethods
def counter_cache_column?(name) # :nodoc:
def counter_cache_column?(name) # :nodoc: _counter_cache_columns.include?(name) end
def decrement_counter(counter_name, id, by: 1, touch: nil)
# and update the updated_at value.
# Decrement the posts_count column for the record with an id of 5
DiscussionBoard.decrement_counter(:posts_count, 5, by: 3)
by a specific amount.
# Decrement the posts_count column for the record with an id of 5
DiscussionBoard.decrement_counter(:posts_count, 5)
# Decrement the posts_count column for the record with an id of 5
==== 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.
* :by - The amount by which to decrement the value. Defaults to +1+.
* +id+ - The id of the object that should be decremented or an array of ids.
* +counter_name+ - The name of the field that should be decremented.
==== Parameters
1 instead of increasing it.
This works the same as #increment_counter but reduces the column value by
Decrement a numeric field by one, via a direct SQL update.
def decrement_counter(counter_name, id, by: 1, touch: nil) update_counters(id, counter_name => -by, touch: touch) end
def increment_counter(counter_name, id, by: 1, touch: nil)
# and update the updated_at value.
# Increment the posts_count column for the record with an id of 5
DiscussionBoard.increment_counter(:posts_count, 5, by: 3)
# by a specific amount.
# Increment the posts_count column for the record with an id of 5
DiscussionBoard.increment_counter(:posts_count, 5)
# Increment the posts_count column for the record with an id of 5
==== 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.
* :by - The amount by which to increment the value. Defaults to +1+.
* +id+ - The id of the object that should be incremented or an array of ids.
* +counter_name+ - The name of the field that should be incremented.
==== Parameters
number of posts and comments there are, each time it is displayed.
posts_count and comments_count to avoid running an SQL query to calculate the
used to store aggregate values. For example, a +DiscussionBoard+ may cache
This method is used primarily for maintaining counter_cache columns that are
Increment a numeric field by one, via a direct SQL update.
def increment_counter(counter_name, id, by: 1, touch: nil) update_counters(id, counter_name => by, touch: touch) end
def load_schema! # :nodoc:
def load_schema! # :nodoc: super association_names = _reflections.filter_map do |name, reflection| next unless reflection.belongs_to? && reflection.counter_cache_column name.to_sym end self.counter_cached_association_names |= association_names end
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
def update_counters(id, counters)
# `updated_at` = '2016-10-13T09:59:23-05:00'
# SET comments_count = COALESCE(comments_count, 0) + 1,
# UPDATE posts
# Executes the following SQL:
Post.update_counters [10, 15], comments_count: 1, touch: true
# and update the updated_at value for each counter.
# For the Posts with id of 10 and 15, increment the comments_count by 1
# WHERE id IN (10, 15)
# SET comments_count = COALESCE(comments_count, 0) + 1
# UPDATE posts
# Executes the following SQL:
Post.update_counters [10, 15], comments_count: 1
# For the Posts with id of 10 and 15, increment the comments_count by 1
# WHERE id = 5
# actions_count = COALESCE(actions_count, 0) + 1
# SET comments_count = COALESCE(comments_count, 0) - 1,
# UPDATE posts
# Executes the following SQL:
Post.update_counters 5, comments_count: -1, actions_count: 1
# increment the actions_count by 1
# For the Post with id of 5, decrement the comments_count by 1, and
==== Examples
attributes.
If attribute names are passed, they are updated along with updated_at/on
* :touch option - Touch timestamp columns when updating.
to update as keys and the amount to update the field by as values.
* +counters+ - A Hash containing the names of the fields
* +id+ - The id of the object you wish to update a counter on or an array of ids.
==== Parameters
given by the corresponding value:
with the given ID, altering the given hash of counters by the amount
be useful on its own. It simply does a direct SQL update for the record
used by #increment_counter and #decrement_counter, but which may also
A generic "counter updater" implementation, intended primarily to be
def update_counters(id, counters) id = [id] if composite_primary_key? && id.is_a?(Array) && !id[0].is_a?(Array) unscoped.where!(primary_key => id).update_counters(counters) end