Question about Associations

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

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

On 4/20/06, Brian V Hughes [email protected] 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 :slight_smile:

Thanks again

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 :slight_smile:

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

On 4/20/06, [email protected] [email protected] 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 :slight_smile:

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

jt