title: Rails has_many through polymorphic counter cache
body: <p>I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box.</p>
  
  <pre><code>class Classifiable &lt; ActiveRecord::Base
    has_many :classifications, :as =&gt; :classifiable, :foreign_key =&gt; :classifiable_id
  end
  
  class Taxonomy &lt; ActiveRecord::Base
    has_many :classifications, :as =&gt; :taxonomy, :foreign_key =&gt; :taxonomy_id
  end
  
  class Question &lt; Classifiable
    has_many :categories, :through =&gt; :classifications, :as =&gt; :classifiable, :source =&gt; :taxonomy, :source_type =&gt; "Category"
  end
  
  class Category &lt; Taxonomy
    has_many :questions, :through =&gt; :classifications, :source =&gt; :classifiable, :source_type =&gt; "Question"
  end
  
  class Classification &lt; ActiveRecord::Base
    attr_accessible :classifiable, :classifiable_id, :classifiable_type,
                    :taxonomy, :taxonomy_id, :taxonomy_type
  
    belongs_to :classifiable, :polymorphic =&gt; true
    belongs_to :taxonomy,     :polymorphic =&gt; true
  end
  </code></pre>