Closed jurek7 closed 12 years ago
Can you elaborate please?
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?
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.
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"
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.
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.
Thank you so much jurek7.
Is this possible to use rails counter_cache for comments?