Forum: Ruby on Rails setting up proper relationships

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pawel J. (Guest)
on 2007-07-16 17:38
Hi

I am trying to make a small survey app and started to get a little
confused. So far i have

survey.rb
has_many :answers

answer.rb
belongs_to :survey

So Survey will have many answers, and answer will belong to only one
survey. This part works fine. Now i want to record survey results (you
can only pick ONE answer, it will NOT be a multiple answer survey).

To do this, as far as i know i should create anwsers_surveys table and
setup proper habtm relations or create a new model and go with :through.


Is this the way to go?, since i am not really trying to make a real
habtm between surveys and answers but need to record the results.
Chris H. (Guest)
on 2007-07-16 17:48
(Received via mailing list)
why not just have a score column in the answers table?
Ilan B. (Guest)
on 2007-07-16 18:23
Pawel Jur wrote:

>
> Is this the way to go?, since i am not really trying to make a real
> habtm between surveys and answers but need to record the results.

Pawel,

Step back from ActiveRecord for a bit and examine your problem domain.
If a survey can have multiple answers and and answer can have multiple
surveys then you have already answered your own question!  i.e.  you
will need a link table either with the habtm (deprecated) or the first
class model approach (:through)

I write this response with the assumption that "answer" is equivalent to
"result".  If I am mistaken then please forgive the confusion.

HTH ilan
Pawel J. (Guest)
on 2007-07-16 20:38
Thanks for responses!

Chris H. wrote:
> why not just have a score column in the answers table?

Yup. So simple solution and it fits just right for my problem. I gotta
start drawing my models and relations on paper before i start making
decisions i guess.


Ilan,

You are right if a survey could have multiple anwsers and answer can
have multiple  surveys but that's not the case. Chris solution fits.
This topic is locked and can not be replied to.