I have the following model:
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
It is leveraged in the Post model as follows:
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
I want keep counts for two things: 1) The number of entity_tags for a particular post_id. This can be done via :counter_cache on the :post in the EntityTag model. 2) I also want to keep track of the number of UNIQUE EntityTags based on unique :entity_id values for a particular post_id. How do I go about achieving this? Can I use :counter_cache with some sort of constraint? Or is there a completely different approach?