# Schema and relationships between tables

**URL:** <https://www.ruby-forum.com/t/schema-and-relationships-between-tables/152820>\
**Category:** Rails\
**Created:** [November 7, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/schema-and-relationships-between-tables/152820 "2008-11-07T18:51:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 7, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/schema-and-relationships-between-tables/152820/1 "2008-11-07T18:51:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 7, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/schema-and-relationships-between-tables/152820/2 "2008-11-07T20:30:33Z")

</div>

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/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

On Fri, Nov 7, 2008 at 2:51 PM, Shandy N.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 7, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/schema-and-relationships-between-tables/152820/3 "2008-11-07T22:15:50Z")

</div>

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
