So, I have an application with two different models, comments and answers, each of which you can either agree or disagree, so I have a polymorphic model called Emotion. Here is my code for them:
class Comment < ActiveRecord::Base belongs_to :user has_many :replies has_many :emotions, :as => :emotionable end class Reply < ActiveRecord::Base belongs_to :user belongs_to :comment has_many :emotions, :as => :emotionable end class Emotion < ActiveRecord::Base belongs_to :emotionable, :polymorphic => :true end
So, everything works fine, but I need to add a cache counter for comments and response in order to get the size of the agreement and disagree for each object. In all documents, he has examples for maintaining a counting cache with normal polymorphic associations, and not with an additional condition. For reference, the diagram for Emotion is as follows:
create_table "emotions", :force => true do |t| t.integer "user_id" t.string "emotion" t.integer "emotionable_id" t.string "emotionable_type" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end
TL: DR - I need to be able to call @ commet.agrees_count, @ comment.disagrees_count, @ reply.agrees_count and @ reply.disagrees_count in a polymorphic association through a cache counter. Thus, the comment and response will require 2 cache counters.
source share