Module: Reusable::Mongoid::CounterCache::ClassMethods
- Defined in:
- lib/reusable/mongoid/counter_cache.rb
Instance Method Summary collapse
Instance Method Details
#counter_cache(options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/reusable/mongoid/counter_cache.rb', line 16 def counter_cache() name = [:name] counter_field = [:field] after_create do |document| relation = document.send(name) relation.collection.update(relation._selector, {'$inc' => {counter_field.to_s => 1}}) end after_destroy do |document| relation = document.send(name) relation.collection.update(relation._selector, {'$inc' => {counter_field.to_s => -1}}) end end |