# Question about Associations

**URL:** <https://www.ruby-forum.com/t/question-about-associations/58368>\
**Category:** Rails\
**Created:** [April 20, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/question-about-associations/58368 "2006-04-20T23:21:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [April 20, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/question-about-associations/58368/1 "2006-04-20T23:21:57Z")

</div>

Hi all. Got a stupid-simple question about associations. I have two  
models - school and course. There are a fixed number of schools (set  
up in the migration). Each course is assigned a school and a school  
will be associated with multiple courses…

How do I set up the associations? Do (can) I have School :has\_one  
:course and Course :has\_many :schools?

Does the schools table then get a course\_id field or the other way  
around?

I don’t think I should have course :belongs\_to School, as there will  
only be one instance of a school in the Schools table.

Thanks!

jt

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [April 20, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/question-about-associations/58368/2 "2006-04-20T23:40:22Z")

</div>

On Apr 20, 2006, at 05:16 PM, John T. wrote:

> Hi all. Got a stupid-simple question about associations. I have two  
> models - school and course. There are a fixed number of schools (set  
> up in the migration). Each course is assigned a school and a school  
> will be associated with multiple courses…
> 
> How do I set up the associations? Do (can) I have School :has\_one  
> :course and Course :has\_many :schools?

If “a school will be associated with multiple courses”, then School  
has\_many: courses, which means Course belongs\_to :school. The fact  
that there are a fixed number of schools is actually irrelevant to  
how the two tables of data relate to each other. You only use has\_one  
when a row from table a is related to one, and only one, row from  
table b. That’s not what you’ve described here.

> Does the schools table then get a course\_id field or the other way  
> around?

Again, if a “course is assigned a [to a] school”, then the courses  
table has a shool\_id field. This is also specified by the association.

> I don’t think I should have course :belongs\_to School, as there will  
> only be one instance of a school in the Schools table.

Honestly, I’ve never thought about which model I “wanted” to have the  
belongs\_to association defined in. That was always done naturally by  
the nature of the relation. Obviously, in a 1-to-1 relation, it’s  
possible to swap the has\_one and belongs\_to, but I would say that  
which ever model you expect to refer to, most often, gets the  
has\_one, and the not-most-often model gets the belongs\_to.

-Brian

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [April 21, 2006, 12:07am UTC](https://www.ruby-forum.com/t/question-about-associations/58368/3 "2006-04-21T00:07:58Z")

</div>

On 4/20/06, Brian V Hughes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If “a school will be associated with multiple courses”, then School  
> has\_many: courses, which means Course belongs\_to :school. The fact  
> that there are a fixed number of schools is actually irrelevant to  
> how the two tables of data relate to each other. You only use has\_one  
> when a row from table a is related to one, and only one, row from  
> table b. That’s not what you’ve described here.

Thanks for the info. I think I did get it figured out, with some help  
from the IRC chat. I have it set up with school :has\_many courses and  
the courses table will have a school\_id. And course :belongs\_to  
schools.

> > Does the schools table then get a course\_id field or the other way  
> > around?
> 
> Again, if a “course is assigned a [to a] school”, then the courses  
> table has a shool\_id field. This is also specified by the association.

Correct, as was pointed out to me.

> -Brian

I was looking too close to the problem - I was getting a little  
confused because I knew that the model that had the “belongs\_to” is  
the one that has the foreign key (per the docs). Just took a second  
opinion to see the light 🙂

Thanks again

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [April 21, 2006, 12:14am UTC](https://www.ruby-forum.com/t/question-about-associations/58368/4 "2006-04-21T00:14:18Z")

</div>

Hi –

On Thu, 20 Apr 2006, John T. wrote:

> > > How do I set up the associations? Do (can) I have School :has\_one  
> > > :course and Course :has\_many :schools?

A small, but not really small, point: it’s has\_one and has\_many, not  
:has\_one and :has\_many. They’re method calls, not symbols 🙂

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [April 21, 2006, 12:17am UTC](https://www.ruby-forum.com/t/question-about-associations/58368/5 "2006-04-21T00:17:27Z")

</div>

On 4/20/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > How do I set up the associations? Do (can) I have School :has\_one  
> > > > :course and Course :has\_many :schools?
> 
> A small, but not really small, point: it’s has\_one and has\_many, not  
> :has\_one and :has\_many. They’re method calls, not symbols 🙂

Heh… of course. Got a little over zealous with the colon key 😉

jt
