1 comment system for multiple models

Hi all,

I have a commenting system I use on different models (posts, reports,
events) meaning that I have some difficulties using depend => destroy
and counter cache.

I now use a sub_id in my comment indicating whether the comment
belongs to a post, event or report eg:
Events table
id | owner_id | category_id | text
0 1 post …
1 1 event …
2 2 report …
3 2 post …

Where the owner_id is the id own the post, event or report.

This approach makes it hard to use either counter cache or depend =>
destroy since it will only look at the owner_id. If a user deletes
post 1 it will also delete the comments of both event 1 and report 1.

Someone has a better approach to using 1 comment system for multiple
models or can I overwrite counter cache and depend destoy to make use
of scoping?

All help is greatly appreciated.

Stijn

What you want to use here are polymorphic associations.
They work mostly like what you have done, using two
columns, one for the id, the other for the type.
commentable_id
commentable_type
in your case.

Then you could define associations like
has_many :comments, :as => :commentable, :dependent => :destroy

and Rails would be able to handle al the sub tasks as
it does with common associations.

read the details here:

since it’s a bit too complex to explain it here