Кэш счетчика для модели с ассоциацией «многие ко многим»

у меня естьПочта иТег модель сmany-to-many ассоциации:

post.rb:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tag_names

  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings

  attr_writer :tag_names
  after_save :assign_tags

  def tag_names
    @tag_names || tags.map(&:name).join(" ")
  end

  private

  def assign_tags
    ntags = []
    @tag_names.to_s.split(" ").each do |name|
      ntags << Tag.find_or_create_by_name(name)
    end
    self.tags = ntags
  end
end

tag.rb:

class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy  
  has_many :posts, :through => :taggings
  has_many :subscriptions
  has_many :subscribed_users, :source => :user, :through => :subscriptions
end

tagging.rb (модель для таблицы соединений):

class Tagging < ActiveRecord::Base
  belongs_to :post  
  belongs_to :tag
end

Я хочу создать:counter_cache который отслеживает, сколько сообщений имеет тег.

Как я могу достичь этого в этой ассоциации многих ко многим?

РЕДАКТИРОВАТЬ:

Я сделал это раньше:

comment.rb:

belongs_to :post, :counter_cache => true

Но теперь, когда нетbelongs_to вpost.rb файл. Я немного смущен.

Ответы на вопрос(2)

Ваш ответ на вопрос