Method: Mongoid::Relations::CounterCache::ClassMethods#reset_counters
- Defined in:
- lib/mongoid/relations/counter_cache.rb
#reset_counters(id, *counters) ⇒ Object
Reset the given counter using the .count() query from the db. This method is usuful in case that a counter got corrupted, or a new counter was added to the collection.
34 35 36 37 38 39 40 41 42 |
# File 'lib/mongoid/relations/counter_cache.rb', line 34 def reset_counters(id, *counters) document = id.is_a?(Document) ? id : find(id) counters.each do |name| = reflect_on_association(name) inverse = .klass.reflect_on_association(.inverse) counter_name = inverse.counter_cache_column_name document.update_attribute(counter_name, document.send(name).count) end end |