Forum: Ruby on Rails Question about Associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-20 23:21
(Received via mailing list)
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V Hughes (Guest)
on 2006-04-20 23:40
(Received via mailing list)
On Apr 20, 2006, at 05:16 PM, John Tsombakos 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
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-21 00:07
(Received via mailing list)
On 4/20/06, Brian V Hughes <brianvh@alum.dartmouth.org> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-21 00:14
(Received via mailing list)
Hi --

On Thu, 20 Apr 2006, John Tsombakos 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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
B07c187ac858535358faa9365b66e657?d=identicon&s=25 John Tsombakos (Guest)
on 2006-04-21 00:17
(Received via mailing list)
On 4/20/06, dblack@wobblini.net <dblack@wobblini.net> 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
This topic is locked and can not be replied to.