jackdempsey / acts_as_commentable

The ActiveRecord acts_as_commentable plugin
http://juixe.com/svn/acts_as_commentable/
MIT License
834 stars 176 forks source link

counter_cache #24

Closed jurek7 closed 12 years ago

jurek7 commented 12 years ago

Is this possible to use rails counter_cache for comments?

jackdempsey commented 12 years ago

Can you elaborate please?

jurek7 commented 12 years ago

Comments use polymorphic relation:

belongs_to :commentable, :polymorphic => true

my model is "Post" and i want to add a column comments_count for this and use counter_cache built-in rails functionality which should increment / decrement this column after add / remove comment. Did anyone check that polymorphic relation works with counter_cache?

jackdempsey commented 12 years ago

Hm. This suggests it might work:

http://stackoverflow.com/questions/2476178/rails-counter-cache-and-its-implementation

relevant line:

belongs_to :resource, :polymorphic => true, :counter_cache => true

if you can provide a working pull request I'd certainly take a look at it!

On Tue, Aug 14, 2012 at 12:30 AM, jurek7 notifications@github.com wrote:

Comments use polymorphic relation:

belongs_to :commentable, :polymorphic => true

my model is "Post" and i want to add a column comments_count for this and use counter_cache built-in rails functionality which should increment / decrement this column after add / remove comment. Did anyone check that polymorphic relation works with counter_cache?

— Reply to this email directly or view it on GitHubhttps://github.com/jackdempsey/acts_as_commentable/issues/24#issuecomment-7718370.

jurek7 commented 12 years ago

I checked this on Rails 3.2.3 and it works. To use counter_cache only need to add:

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

to comment model. And for related models generate migration which add a column ":counter_column_name"

jackdempsey commented 12 years ago

great, have time for a pull request?

On Wed, Aug 15, 2012 at 11:40 PM, jurek7 notifications@github.com wrote:

I checked this on Rails 3.2.3 and it works. To use counter_cache only need to add:

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

to comment model. And for related models generate migration which add a column ":counter_column_name"

— Reply to this email directly or view it on GitHubhttps://github.com/jackdempsey/acts_as_commentable/issues/24#issuecomment-7777845.

blimey85 commented 11 years ago

Just wanted to comment that this worked for me. I called my column :comments_count figuring that this would work for me no matter which model I was attaching comments to (goals, posts, whatever).

Regarding a pull request (if this hasn't already been done), would have this in there by default cause a problem if the column did not exist? Or would the default move then to including both this additional bit of code and the column? I would assume getting comment counts is a common thing.

maheshd1991 commented 8 years ago

Thank you so much jurek7.