# Making a generic comment model

**URL:** <https://www.ruby-forum.com/t/making-a-generic-comment-model/184820>\
**Category:** Rails\
**Created:** [March 9, 2010, 2:48am UTC](https://www.ruby-forum.com/t/making-a-generic-comment-model/184820 "2010-03-09T02:48:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [March 9, 2010, 2:48am UTC](https://www.ruby-forum.com/t/making-a-generic-comment-model/184820/1 "2010-03-09T02:48:02Z")

</div>

I have a Comment model obviously to make comments. I have also a  
Person and Family class and I want to make comments on bother a person  
and a family. How can I set this up. I want to use the same generic  
comments table for all the comments on both person and family model. I  
can’t figure it out, I can set up comments for either a Person or a  
Family one at a time but not both.

Comment  
person\_id

Class Person  
has\_many :comments

How can I make comments work on the Family model too?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [March 9, 2010, 4:09am UTC](https://www.ruby-forum.com/t/making-a-generic-comment-model/184820/2 "2010-03-09T04:09:25Z")

</div>

You want to make comment a polymophic association  
e.g. on your person and family model you have:

has\_many :comments, :as =\> :attachable

then on the comment class you have:

belongs\_to :attachable, :polymorphic =\> true

and in your table for comments you have:

integer :attachable\_id  
string :attachable\_type  
text :comment

Best, Daniel

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [March 9, 2010, 4:21am UTC](https://www.ruby-forum.com/t/making-a-generic-comment-model/184820/3 "2010-03-09T04:21:42Z")

</div>

thanks you! polymorphic association it’s called you say. It works  
now.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [March 9, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/making-a-generic-comment-model/184820/4 "2010-03-09T13:50:37Z")

</div>

On Mon, Mar 8, 2010 at 10:20 PM, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks you! polymorphic association it’s called you say. It works  
> now.

You might want to have a look at the acts\_as\_commentable plugin

> **[GitHub - jackdempsey/acts\_as\_commentable: The ActiveRecord...](https://github.com/jackdempsey/acts_as_commentable)**
>
> The ActiveRecord acts\_as\_commentable plugin. Contribute to jackdempsey/acts\_as\_commentable development by creating an account on GitHub.

It expands on this idea, but uses commentable rather than attachable  
as the association name, which I think makes a bit more sense, and is  
less likely to collide if you decide that one or more of the models  
which can have comments can also have uploaded files as attachments.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
