STI vs Polymorphism

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


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.

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


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
-- Andres Paglayan
Testi. Codi. Vinci.