Polymorphic Relationships and Cache Counter

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.

+6
source share
3 answers

My suggestion was to manually increment or decrement the cache counter in the after_commit callback so that you can check if the record was saved and updated outside the transaction. This is because it will make your code more explicit and less cryptic in how and when the cache is updated or invalid.

Also, manually updating the cache gives you additional flexibility if you would like to give some users more authority when they agree or disagree with the comment (for example, karma systems).

+3
source

you can add the counter cache attribute to the attr_readonly list in related classes (for example, the Post class; attr_readonly: comments_count; end). http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to

 :polymorphic Specify this association is a polymorphic association by passing true. Note: If you've enabled the counter cache, then you may want to add the counter cache attribute to the attr_readonly list in the associated classes (eg class Post; attr_readonly :comments_count; end). 
+4
source

This does not apply to Polymorphic Relationships and Cache Accounts , o Multiple counter_cache in the Rails Model

By the way, for Polymorphic Relations and Counting Cache

 class Code < ActiveRecord::Base has_many :notes, :as => :noteable end class Issue < ActiveRecord::Base has_many :notes, :as => :noteable end class Note < ActiveRecord::Base belongs_to :noteable, polymorphic: true, count_cache: :noteable_count end 

in your problems with tables, you should have a column "noteable_count", the same as your "table codes"

+1
source

Source: https://habr.com/ru/post/911334/


All Articles