Forum: Ruby on Rails Schema and relationships between tables

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.
Shandy N. (Guest)
on 2008-11-07 19:51
I am trying to allow a user to answer some questions and then save those
questions but I am unsure if I am getting the layout correct. Here is
what I have so far:

There is a questions table holding the questions to ask

There is an answers tables which holds the answers to the questions
asked of a user.

Then there is of course the Users table which hold a variety of user
info

I figure my migrations would look something like:

Questions: has_many :answers

Answers: belong_to :user, belongs_to :question

User has_many :answers

so my table would look like:

User: id, username, . . .

Questions: id, question

Answer: id, user_id, question_id, answer

Where I am a little confused is if the belongs_to :question should
rather be a has_one :question and would that change the schema at all?
Also, can anyone point me to some good online tutorials involving Model
Relationships, thanks,

-S
Maurício L. (Guest)
on 2008-11-07 21:30
(Received via mailing list)
When you "belong_to" it means that the foreing key is at your side,
you belong to the question 'cos you wouldn't be able to exist without
it, there's no reason to have a question without a user or an answer.

A has_one means that you have of of that, but you usually don't need
it to exist. it also means that the foreign key lives at the other
side.

Look for weak and strong entities in database design

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208



On Fri, Nov 7, 2008 at 2:51 PM, Shandy N.
Marli B. (Guest)
on 2008-11-07 23:15
Shandy N. wrote:
> I am trying to allow a user to answer some questions and then save those
> questions but I am unsure if I am getting the layout correct. Here is
> what I have so far:
>
> There is a questions table holding the questions to ask
>
> There is an answers tables which holds the answers to the questions
> asked of a user.
>
> Then there is of course the Users table which hold a variety of user
> info
>
> I figure my migrations would look something like:
>
> Questions: has_many :answers
>
> Answers: belong_to :user, belongs_to :question
>
> User has_many :answers
>
> so my table would look like:
>
> User: id, username, . . .
>
> Questions: id, question
>
> Answer: id, user_id, question_id, answer
>
> Where I am a little confused is if the belongs_to :question should
> rather be a has_one :question and would that change the schema at all?
> Also, can anyone point me to some good online tutorials involving Model
> Relationships, thanks,
>
> -S

For what it's worth, I would probably do something like:

User:  has_many :questions
       has_many :answers

Question:  has_many :answers
           belongs_to :user

Answer:  belongs_to :question
         belongs_to :user

Tables:
User:     id | user_name
Question: id | user_id | question
Answer:   id | user_id | question_id | answer


hope this helps
This topic is locked and can not be replied to.