页面模型
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当我编辑页面和更改页面类别时,它不会更新pages_count。是我漏掉了什么,还是这很正常?我怎么才能让它工作呢?
更新:
我在categories表上有pages_count列,计数器缓存在创建和销毁后工作。
发布于 2011-02-28 19:51:51
我找到了解决方案。此问题已在Rails 3.1.0.beta中修复。也许它能帮助其他有同样问题的人。我花了4个小时才弄清楚。Commit is here
发布于 2011-02-28 08:25:54
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
根据文档,您需要在页表中添加一个列,比如#{table_name}_count。
运行新的迁移:
script/generate migration add_category_count_to_pages然后添加以下内容进行迁移:
add_column :pages, :categories_count, :integer然后是rake db:migrate
这应该会解决这个问题。
更新
计数器缓存需要放在关联表上:
categorization.rb型号:
belongs_to :page, :counter_cache => :pages_count
belongs_to :categoryhttps://stackoverflow.com/questions/5137076
复制相似问题