# Model design question

**URL:** <https://www.ruby-forum.com/t/model-design-question/112075>\
**Category:** Rails\
**Created:** [August 15, 2007, 12:21am UTC](https://www.ruby-forum.com/t/model-design-question/112075 "2007-08-15T00:21:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@r.g](https://www.ruby-forum.com/u/r.g)\
**Post date:** [August 15, 2007, 12:21am UTC](https://www.ruby-forum.com/t/model-design-question/112075/1 "2007-08-15T00:21:17Z")

</div>

Hello,

I’m creating an app that will collect and display user-submitted  
comments for several different content types. They comment structure  
will be the same for each type.

I see two ways of doing this:

1. define belongs\_to relationships with all the different content  
types in my comment.rb model:

belongs\_to :type\_a  
belongs\_to :type\_b  
…

This would also require a number of foreign keys in my comment table:

type\_a\_id :integer,  
type\_b\_id :integer,  
…

1. create a model for each comment application:

CommentTypeA  
CommentTypeB

I suppose I am worried about dealing with multiple foreign keys in  
approach 1), and I also like the idea of having each comment type  
contained in its own class and table, for easier modification down the  
road in (second approach). And maybe validation would be easier with  
the second approach. But on the other hand, 2) is a lot of repetition  
(which of course is so not DRY), and could prove to be risky for other  
reasons.

Does anyone with some related experience have any advice?

Thanks very much.

---

<div class="post-metadata">

**Author:** ![r.g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@r.g](https://www.ruby-forum.com/u/r.g)\
**Post date:** [August 15, 2007, 12:36am UTC](https://www.ruby-forum.com/t/model-design-question/112075/2 "2007-08-15T00:36:18Z")

</div>

r.g. wrote:

> Does anyone with some related experience have any advice?

Polymorphic relationships should work for you…

class Comment  
belongs\_to :comentable, :polymorphic =\> true

class TypeA  
has\_many :comments, :as =\> :commentable

schema…  
commentable\_type :string  
commentable\_id :integer

–  
[http://www.5valleys.com/](http://www.5valleys.com/)

---

<div class="post-metadata">

**Author:** ![r.g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@r.g](https://www.ruby-forum.com/u/r.g)\
**Post date:** [August 15, 2007, 1:06am UTC](https://www.ruby-forum.com/t/model-design-question/112075/3 "2007-08-15T01:06:19Z")

</div>

Thanks a lot! That seems to be working well
