Ruby on rails Mongoid 3.1.0计数器缓存不';行不通
我正在尝试使用Mongoid CounterCache,但它不起作用 我试着用Ruby on rails Mongoid 3.1.0计数器缓存不';行不通,ruby-on-rails,mongoid,counter-cache,Ruby On Rails,Mongoid,Counter Cache,我正在尝试使用Mongoid CounterCache,但它不起作用 我试着用 belongs_to :user, counter_cache: true 但它又回来了 Problem: Invalid option :counter_cache provided to relation :user. Summary: Mongoid checks the options that are passed to the relation macros to ensure that no il
belongs_to :user, counter_cache: true
但它又回来了
Problem:
Invalid option :counter_cache provided to relation :user.
Summary:
Mongoid checks the options that are passed to the relation macros to ensure that no ill side effects occur by letting something slip by.
Resolution:
Valid options are: autobuild, autosave, dependent, foreign_key, index, polymorphic, touch, class_name, extend, inverse_class_name, inverse_of, name, relation, validate, make sure these are the ones you are using.
于是我又加了一句
include Mongoid::CounterCache
重新启动我的Web服务器,然后重试,但返回
uninitialized constant Mongoid::CounterCache
对这个问题有什么想法吗?我遇到了同样的事情。这是对我有用的东西 假设您的应用程序中已经有这些类,并且您决定稍后添加计数器缓存。因此,您将
计数器\u cache:true
添加到您的子类中
类用户
include Mongoid::Document
字段:名称,类型:字符串
你有很多东西吗
结束
阶级事务
include Mongoid::Document
字段:名称,类型:字符串
属于:用户,计数器\u缓存:true
结束
然后跳入控制台并执行以下操作:
u=User.first
u、 事物。计数#=>10
u、 things_count#=>nomethoder错误:未定义的方法things_count
User.update\u计数器(u.id,things\u count:u.things.count)
u、 重新装载
u、 事情的数量#=>10
如果有人有一个更简单或更干净的方法来做这件事,那就太棒了。它很管用。但每次我给一个新孩子初始化时都必须这样做吗。因为没有它,它似乎不起作用。还是我有两个父母的问题?