lib/mongoid/association/referenced/counter_cache.rb
# Reset the given counter using the .count() query from the
# db. This method is useful in case that a counter got
# corrupted, or a new counter was added to the collection.
#
# @example Reset the given counter cache
#   Post.reset_counters('50e0edd97c71c17ea9000001', :comments)
#
# @param [ String ] id The id of the object that will be reset.
# @param [ Symbol, Array ] counters One or more counter caches to reset
Reset the given counter using the .count() query from the
db. This method is useful in case that a counter got
corrupted, or a new counter was added to the collection.
Reset the given counter using the . count () query from the db . This method is useful in case that a counter got corrupted or a new counter was added to the collection .
def reset_counters(id, *counters)
            document = id.is_a?(Document) ? id : find(id)
            counters.each do |name|
              relation_association = relations[name]
              counter_name = relation_association.inverse_association.counter_cache_column_name
              document.update_attribute(counter_name, document.send(name).count)
            end
          end
def reset_counters ( id , * counters ) document = id . is_a? ( Document ) ? id : find ( id ) counters . each do | name | relation_association = relations [ name ] counter_name = relation_association . inverse_association . counter_cache_column_name document . update_attribute ( counter_name , document . send ( name ) . count ) end end
Reset the given counter using the .count() query from the
db.
Reset the given counter using the . count () query from the db .
id
<not_specific>
counters
<not_specific>
<not_specific>
counters One or more counter caches to reset
counters One or more counter caches to reset
example
Reset the given counter cache Post.reset_counters('50e0edd97c71c17ea9000001', :comments)
Reset the given counter cache Post . reset_counters ( ' 50e0edd97c71c17ea9000001 ' : comments )