elight / acts_as_commentable_with_threading

Similar to acts_as_commentable; however, utilizes awesome_nested_set to provide threaded comments
MIT License
671 stars 168 forks source link

How to use counter_cache with acts_as_commentable and rails 4.2 #103

Open eduardm opened 8 years ago

eduardm commented 8 years ago

I think it will be useful to be able to use counter_cache with acts_as_commentable, could save a lot of processing time on large sites

bufordtaylor commented 7 years ago

Should work smoothly like normal

add_column :projects, :comments_count, :integer, :default => 0
Project.all.find_each do |p|
   Project.update_counters p.id, :comments_count => p.root_comments.length
end

then in comment.rb

  belongs_to :commentable, :polymorphic => true, :counter_cache => :comments_count