# STI good or bad for my problem?

**URL:** <https://www.ruby-forum.com/t/sti-good-or-bad-for-my-problem/167762>\
**Category:** Rails\
**Created:** [May 12, 2009, 11:13am UTC](https://www.ruby-forum.com/t/sti-good-or-bad-for-my-problem/167762 "2009-05-12T11:13:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecccb3/32.png) [@fredd](https://www.ruby-forum.com/u/fredd)\
**Post date:** [May 12, 2009, 11:13am UTC](https://www.ruby-forum.com/t/sti-good-or-bad-for-my-problem/167762/1 "2009-05-12T11:13:27Z")

</div>

Hi. I am currently developing a small course system for student and  
teachers. The system is divided in sections and sections have many  
messages, comments, documents and schedules. Pretty easy to set up  
using ROR, just add some belongs\_to and has\_many relations. But  
everything becomes much more complicated when the section items  
(message, comment and so on…) all has comments. And they can also be  
a favorite to a users favorite list and much more. Basically all the  
items has some shared generic stuff that I just don’t want to copy  
around. Let’s check my rest routes so everyone understands better:

map.resources :sections do |sections|  
sections.resources :messages  
sections.resources :documents  
sections.resources :schedules  
end

map.resources :items do |item|  
item.resources :comments  
item.resources :favorites  
end

I would basically want to have a generic resource (Item) that takes  
care of comments, favorites and so on. And that is what my question is  
all about. Is it a good way to inherit from a base class called Items  
using STI? Or could I solve this problem some other way? If I don’t  
have a base class I would have to nest the comment resources to each  
of the items and that’s not a good solution either.

---

<div class="post-metadata">

**Author:** ![fredd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecccb3/32.png) [@fredd](https://www.ruby-forum.com/u/fredd)\
**Post date:** [May 12, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/sti-good-or-bad-for-my-problem/167762/2 "2009-05-12T12:00:31Z")

</div>

Hello,  
STI seems not suitable, I would call that abuse, unless you have almost  
everything common between yout different tables.

But you can use o polymorphic relation for comment, and extract the  
common  
behaviour in a plugin (something like acts\_as\_commentable)

Look here : hhttp://  
guides.rails.info/association\_basics.html#polymorphic-associations

---

<div class="post-metadata">

**Author:** ![fredd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecccb3/32.png) [@fredd](https://www.ruby-forum.com/u/fredd)\
**Post date:** [May 12, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/sti-good-or-bad-for-my-problem/167762/3 "2009-05-12T12:58:05Z")

</div>

Yes, I was almost thinking that my self. Thanks for clarifying that.  
What I now is trying to do is to have a generic model called Item that  
relates to the different items through a itemable interface  
(belongs\_to :itemable, polymorphic =\> true). And then the Item model  
belongs to the Section. What do you think of that?
