title: Polymorphic relationships and counter cache
body: <p>So I have an app with a 2 different models, Comments and Replies, each of which you can either Agree or Disagree, so I have a polymorphic model called Emotion. Here is my code for these:</p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :user
    has_many :replies
    has_many :emotions, :as =&gt; :emotionable
  end
  
  
  
  class Reply &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :comment
    has_many :emotions, :as =&gt; :emotionable
  end
  
  class Emotion &lt; ActiveRecord::Base
    belongs_to :emotionable, :polymorphic =&gt; :true  
  end
  </code></pre>
  
  <p>So this all works fine, but I'm going to need to add a counter cache for both Comment and Reply in order to get the size of the Agrees and Disagree for each Object. In all of the docs, it has examples for doing counter cache with normal polymorphic associations, not one with an extra condition in it. For reference, by schema for Emotion looks like this:</p>
  
  <pre><code>create_table "emotions", :force =&gt; true do |t|
    t.integer  "user_id"
    t.string   "emotion"
    t.integer  "emotionable_id"
    t.string   "emotionable_type"
    t.datetime "created_at",       :null =&gt; false
    t.datetime "updated_at",       :null =&gt; false
  end
  </code></pre>
  
  <p>TL:DR - I need to be able to call @commet.agrees_count, @comment.disagrees_count, @reply.agrees_count and @reply.disagrees_count on a polymorphic association through a counter cache. So Comment and Reply will need 2 counter caches.</p>