Schema and relationships between tables

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

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

Shandy Nantz 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