2016-07-12 3 views
1

私は3モデル:Topic,Post,Linkがあります。マルチレベル関連のためのcounter_cache

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 

私はLinkモデルにフォーラムのcounter_cacheを持っているしたいと思います。 どうすればいいですか?

答えて

0

counter_culture gemをご覧ください。 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 tableproducts_count欄に最新のカウンターキャッシュを維持します。

関連する問題