2016-07-12 11 views
1

Ich habe 3 Modelle: Topic, Post, Link.counter_cache für multilevel association

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 

Ich möchte auf Link Modell counter_cache für Foren haben. Wie kann ich das tun?

Antwort

0

Überprüfen Sie die counter_culture gem.

Vom 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 

In dem obigen Beispiel das Category Modell hält ein up-to-date Gegen Cache in der products_count Spalte der categories table.