多层关联的counter_cache

4
我有三个模型: Topic, Post, Link
class Topic < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  has_many :links
  belongs_to :topic
end

class Link < ActiveRecord::Base
  belongs_to :post
end

我希望在模型中的论坛上使用counter_cache。我该如何做到这一点?
1个回答

0

看看 counter_culture 宝石

来自 readme

class Product < ActiveRecord::Base
  belongs_to :sub_category
  counter_culture [:sub_category, :category]
end

class SubCategory < ActiveRecord::Base
  has_many :products
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :sub_categories
end

在上面的例子中,Category 模型将在 categories 表的 products_count 列中保持最新的计数缓存。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接