# STI vs Polymorphism

**URL:** <https://www.ruby-forum.com/t/sti-vs-polymorphism/139700>\
**Category:** Rails\
**Created:** [June 6, 2008, 5:39am UTC](https://www.ruby-forum.com/t/sti-vs-polymorphism/139700 "2008-06-06T05:39:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thejackal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/919ad9/32.png) [@thejackal](https://www.ruby-forum.com/u/thejackal)\
**Post date:** [June 6, 2008, 5:39am UTC](https://www.ruby-forum.com/t/sti-vs-polymorphism/139700/1 "2008-06-06T05:39:19Z")

</div>

Hello everyone,

I am racking my brain with a modelling question and can’t wrap my around  
the benefits of STI or Polymorphisms.

I am trying to model Questions and Answer behavior but I think I am not  
using rails full power here.

I have (in simple terms)

Question  
has\_one Answer

Answer  
belongs\_to Question

I also have

Topic  
has\_many Posts

Post  
belongs\_to Topic

Now i am trying to make this really easy for me to Search, do specific  
queries like “Give me all the Questions that Jim asked…” etc.

I tried using a polymorhpic association called Message that acts in  
between these types so each time I create a new Message a end up  
creating one of the Models above.

But then I ran into some open source code and saw an implementation that  
used STI where they had a Message Model inheriting from a Communication  
Model. I like this since the Question, Answer, Topic, Post, they all  
share common things and it would be great to keep things DRY.

That got me thinking.

Is it wrong to do

class Communication \< ActiveRecord:Base  
end

class Message \< Communication  
end

class Question \< Communication  
end

class Answer \< Communication  
end

etc.

This way I could keep everything in one table but then I can’t do easy  
things like Answer.find\_all\_by\_id(5)

I think I am missing something about the behaviors here and would love  
some help.

Thanks

Kent

---

<div class="post-metadata">

**Author:** ![thejackal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/919ad9/32.png) [@thejackal](https://www.ruby-forum.com/u/thejackal)\
**Post date:** [June 6, 2008, 5:53am UTC](https://www.ruby-forum.com/t/sti-vs-polymorphism/139700/2 "2008-06-06T05:53:22Z")

</div>

On Fri, 2008-06-06 at 05:39 +0200, Kent F. wrote:

> Question  
> Post  
> belongs\_to Topic
> 
> Now i am trying to make this really easy for me to Search, do specific  
> queries like “Give me all the Questions that Jim asked…” etc.

nor sti nor poly why not using =\> asked\_questions  
ï»¿it’s a has and belongs to many with through  
user as many questions through asked questions  
then you can say User.asked\_questions  
asked questions belongs to both, user and questions  
user and questions has many asked questions
