2

私は次のモデルを持っています:

class EntityTag < ActiveRecord::Base
  attr_protected :user_id, :post_id, :entity_id

  belongs_to :user
  belongs_to :post
  belongs_to :entity

  validates :user_id, :presence => true
  validates :entity_id, :presence => true
  validates :post_id, :presence => true

  validates_uniqueness_of :user_id, :scope => [:entity_id, :post_id]
end

Post モデルでは次のように利用されます。

class Post < ActiveRecord::Base
  ...
  has_many :entity_tags, :dependent => :destroy
  has_many :tags, :through => :entity_tags, :uniq => true,
    :source => :entity
  has_many :taggers, :through => :entity_tags, :uniq => true,
    :source => :user
  ...
end

1) 特定の post_id の entity_tags の数。これは、EntityTag モデルの :post で :counter_cache を介して実行できます。2) 特定の post_id の一意の :entity_id 値に基づいて、UNIQUE EntityTags の数も追跡したいと考えています。これを達成するにはどうすればよいですか?:counter_cache をある種の制約付きで使用できますか? または、まったく異なるアプローチがありますか?

4

0 に答える 0