# Relationship Advice - who do I belong to & can I have many?

**URL:** <https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904>\
**Category:** Rails\
**Created:** [October 19, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904 "2006-10-19T19:50:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 19, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/1 "2006-10-19T19:50:17Z")

</div>

I am trying to figure out the has / belongs\_to etc relationship for  
rails but everyone always uses very abstract examples. Could someone  
fill in the blanks on the following example I came up with over lunch?

In my actual program I never bothered filling out these relationships  
but thought it might be a good idea to fix that. Do you always fill  
these out and what sort of benifits do you get from it?

Say we have a school with students, teachers, classes and books.

A teacher can teach 1 to 3 classes  
A student takes 6 of 10 offered classes  
Each class has exactly 1 unique book  
[A student:  
Takes some but not all classes,  
Has different teachers, but may have the same teacher multiple times  
Has one book per class or 6 books]

The students could print out their booklist, schedule. The teacher  
could print out a list of their classes and the students in them. etc.

A rough idea for the tables might look like so

Teachers  
id, name…

Student  
id, name…

Class  
id, description…

schedule  
id, teacher\_id, student\_id, class\_id

Book  
id, class\_id

How would the models look? Here is what I came up with

Teacher  
belongs\_to :schedule

Student  
belongs\_to :schedule

Class  
belongs\_to :schedule  
has\_one :book

Schedule  
has\_many :teacher  
has\_many :student  
has\_many :class

Book  
belongs\_to :class

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 19, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/2 "2006-10-19T20:05:41Z")

</div>

On Thu, 19 Oct 2006, Mic wrote:

> I am trying to figure out the has / belongs\_to etc relationship for  
> rails but everyone always uses very abstract examples. Could someone  
> fill in the blanks on the following example I came up with over lunch?

Not your example, but this will probably help…

[http://mboffin.com/stuff/ruby-on-rails-data-relationships.png](http://mboffin.com/stuff/ruby-on-rails-data-relationships.png)

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 19, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/3 "2006-10-19T20:48:20Z")

</div>

That was just the sort of thing I was looking for!

Philip H. wrote:

> Not your example, but this will probably help…
> 
> [http://mboffin.com/stuff/ruby-on-rails-data-relationships.png](http://mboffin.com/stuff/ruby-on-rails-data-relationships.png)

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 20, 2006, 12:23am UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/4 "2006-10-20T00:23:55Z")

</div>

Mic wrote:

> Relationship Advice - who do I belong to & can I have many?

First off, I should point out that ‘belong to’ is a strong term. In any  
good relationship, there should certainly be a sense of belonging  
towards each other, but you shouldn’t ever feel that you merely ‘belong  
to’ somebody, as if you were their property. Otherwise you’ll be  
dependent on them, and what will happen when they’re gone?

With that out of the way, the simple answer is that you belong to your  
‘significant other’, although I admit that that’s something of a  
tautology. Do you have someone locally who responds to ‘girlfriend’,  
‘boyfriend’, ‘wife’, or ‘husband’? ‘Honey’? ‘Snookie-pie’? If you do,  
chances are that you belong to them.

And to get the full benefits of the relationship, you should always  
make sure the association is set up in the other direction as well.

You should note that in the case of ‘wife’ and ‘husband’, a slightly  
more formal API applies:

> **[Marriage vows](https://en.wikipedia.org/wiki/Marriage_vows)**
>
> Marriage vows are promises each partner in a couple makes to the other during a wedding ceremony based upon Western Christian norms. They are not universal to marriage and not necessary in most legal jurisdictions. They are not even universal within Christian marriage, as Eastern Christians do not have marriage vows in their traditional wedding ceremonies.
> In the time of the Roman Empire (17 BC – 476 AD) the lower classes had "free" marriages. The bride's father would deliver her to the groom,...

As for the question of whether you can have many, the answer is  
generally no, particularly in the case of the formal API (although, as  
usual, there are exceptions depending on what environment you’re in).  
Most people just have one.

If you insist on having many, it is possible, and you’ll find that each  
relationship basically works the same as usual. And you can still keep  
your original ‘has one’ relationship – that way, you can refer to one  
of your many (by default, the earliest one) as a special case.

Just make sure you don’t confuse their names.

And you certainly don’t want to find all of them in the same place.  
It’s probably best if you keep them within different scopes. Of course,  
I don’t recommend this kind of chicanery. It is likely to be deprecated  
soon, anyway.

Chris

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 20, 2006, 12:45am UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/5 "2006-10-20T00:45:58Z")

</div>

From my dating experiences, I can also confirm that :has\_many from my  
end results in cache misses. 🙂

Yeah, I know. It’s lame.

Chris M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Mic wrote:

> Relationship Advice - who do I belong to & can I have many?

First off, I should point out that ‘belong to’ is a strong term. In any  
good relationship, there should certainly be a sense of belonging  
towards each other, but you shouldn’t ever feel that you merely ‘belong  
to’ somebody, as if you were their property. Otherwise you’ll be  
dependent on them, and what will happen when they’re gone?

With that out of the way, the simple answer is that you belong to your  
‘significant other’, although I admit that that’s something of a  
tautology. Do you have someone locally who responds to ‘girlfriend’,  
‘boyfriend’, ‘wife’, or ‘husband’? ‘Honey’? ‘Snookie-pie’? If you do,  
chances are that you belong to them.

And to get the full benefits of the relationship, you should always  
make sure the association is set up in the other direction as well.

You should note that in the case of ‘wife’ and ‘husband’, a slightly  
more formal API applies:

> **[Marriage vows](https://en.wikipedia.org/wiki/Marriage_vows)**
>
> Marriage vows are promises each partner in a couple makes to the other during a wedding ceremony based upon Western Christian norms. They are not universal to marriage and not necessary in most legal jurisdictions. They are not even universal within Christian marriage, as Eastern Christians do not have marriage vows in their traditional wedding ceremonies.
> In the time of the Roman Empire (17 BC – 476 AD) the lower classes had "free" marriages. The bride's father would deliver her to the groom,...

As for the question of whether you can have many, the answer is  
generally no, particularly in the case of the formal API (although, as  
usual, there are exceptions depending on what environment you’re in).  
Most people just have one.

If you insist on having many, it is possible, and you’ll find that each  
relationship basically works the same as usual. And you can still keep  
your original ‘has one’ relationship – that way, you can refer to one  
of your many (by default, the earliest one) as a special case.

Just make sure you don’t confuse their names.

And you certainly don’t want to find all of them in the same place.  
It’s probably best if you keep them within different scopes. Of course,  
I don’t recommend this kind of chicanery. It is likely to be deprecated  
soon, anyway.

Chris

* * *

Do you Yahoo!?  
Everyone is raving about the all-new Yahoo! Mail.

---

<div class="post-metadata">

**Author:** ![mic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@mic](https://www.ruby-forum.com/u/mic)\
**Post date:** [October 20, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/relationship-advice-who-do-i-belong-to-can-i-have-many/72904/6 "2006-10-20T18:26:02Z")

</div>

Great post Chris! It was just what I need on a Friday!
