Ruby on rails 更新后计数器缓存不工作

Ruby on rails 更新后计数器缓存不工作,ruby-on-rails,Ruby On Rails,页面模型 has_many :categorization has_many :categories, :through => :categorization 类别模型 has_many :categorization has_many :pages, :through => :categorization 分类模型 belongs_to :page belongs_to :category, :counter_cache => :pages_count 当我编辑页面并更

页面模型

has_many :categorization
has_many :categories, :through => :categorization
类别模型

has_many :categorization
has_many :pages, :through => :categorization
分类模型

belongs_to :page
belongs_to :category, :counter_cache => :pages_count
当我编辑页面并更改页面类别时,它不会更新页面计数。我是错过了什么还是这很正常?我怎样才能让它工作

更新:

我在categories表上有pages\u count列,计数器缓存在创建和销毁后工作。

根据文档,您需要在页面表中添加一列,例如
{table\u name}\u count

运行新迁移:

script/generate migration add_category_count_to_pages
add_column :pages, :categories_count, :integer
然后添加以下内容以执行迁移:

script/generate migration add_category_count_to_pages
add_column :pages, :categories_count, :integer
然后
rakedb:migrate

这就应该解决了

更新 计数器缓存需要放置在关联的表上:

categorization.rb模型:

belongs_to :page, :counter_cache => :pages_count
belongs_to :category

我找到了解决办法。这个问题在Rails 3.1.0.beta中得到了修复。也许它能帮助其他有同样问题的人。我花了4个小时才找到答案。

没有,先生,我把所有的桌子都准备好了。计数器缓存在创建或销毁后工作。看起来您需要将其放在关联的类上,所以是在post上,而不是类别上。