模块:Mongoid::Association::Referenced::CounterCache::ClassMethods
- 定义于:
- lib/mongoid/association/referenced/counter_cache.rb
实例方法摘要折叠
-
# decrement_counter (counter_name, ID ) ⇒ 对象
将与ID匹配的条目中的计数器名称减 1。
-
#increment_counter (counter_name, ID ) ⇒ 对象
将与ID匹配的条目中的计数器名称加 1。
-
# reset_counters ( ID , *counters) ⇒ 对象
使用 .count() 重置给定计数器 从数据库查询。
-
# update_counters ( ID ,counters) ⇒ 对象
按值因子更新给定计数器。
实例方法详细信息
# decrement_counter (counter_name, ID ) ⇒对象
将与ID匹配的条目中的计数器名称减 1。 当启用 counter_cache 时,此方法用于关联回调
84 85 86 |
# File 'lib/mongoid/association/referenced/counter_cache.rb', line 84 def Decrement_counter(counter_name, id) update_counters(id, counter_name.to_sym => -1) end |
#increment_counter (counter_name, ID ) ⇒对象
将与ID匹配的条目中的计数器名称加 1。 当启用 counter_cache 时,此方法用于关联回调
71 72 73 |
# File 'lib/mongoid/association/referenced/counter_cache.rb', line 71 def increment_counter(counter_name, id) update_counters(id, counter_name.to_sym => 1) end |
# reset_counters ( ID , *counters) ⇒对象
使用 .count() 重置给定计数器 从数据库查询。 如果计数器损坏或将新计数器添加到集合中,则此方法非常有用。
39 40 41 42 43 44 45 46 |
# File 'lib/mongoid/association/referenced/counter_cache.rb', line 39 def reset_counters(id, *计数器) 文档 = id.is_a?(文档) ? id : find(id) 计数器.每 do |名称| role_association = 关系[名称] counter_name = role_association.inverse_association.counter_cache_column_name 文档.update_attribute(counter_name, 文档.发送(名称).数数) end end |
# update_counters ( ID ,counters) ⇒对象
按值因子更新给定计数器。 它使用原子 $inc 命令。
58 59 60 |
# File 'lib/mongoid/association/referenced/counter_cache.rb', line 58 def update_counters(id, 计数器) WHERE(:_id => id).inc(计数器) end |