Forum: Ruby on Rails belongs_to, has_one, has_many question (again?)

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.
3948345772146ca07fe74b2bb7d5d630?d=identicon&s=25 Tom de Grunt (Guest)
on 2006-02-20 23:51
(Received via mailing list)
Hi All,

Probably this has been asked numerous times, I apologize already!
Can somebody point me to a good tutorial on how Rails works with
relations?

I know about database design and normalization, I also know about
programming in general (and OOP for that matter).

Only thing I can say is that I'm following the "Four days on Rails"
tutorial by John McCreesh.
He writes on Items and Notes (for his Todo example).

The items table has a key to the note_id. However (and this in
particular I'm trying to understand), the Models which accompany the
tables (item & note) are like:

class Item                class Note
belongs_to: note          # nothing here really
.                         .
.                         .
end                       end

It only struck me that I have the feeling that "it" (belongs_to)
should be the other way around.
I'm actually looking for some page which spills out the relations or
associations within Rails, like:

1:1 relation: has_one, belongs_to in such and such way
1:many relation has_many, belongs_to in such and such way
many:many relation: already has a tutorial of it's own! (http://
jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

Thanks,
Tom
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-21 02:06
(Received via mailing list)
On Feb 20, 2006, at 2:49 PM, Tom de Grunt wrote:

> tutorial by John McCreesh.
> end                       end
>
> Thanks,
> Tom


Tom-

	Here is a link with a lot of info:

http://ar.rubyonrails.com/classes/ActiveRecord/Associations/
ClassMethods.html

	And you have it correct already, the belongs_to is a bit confusing.
It helps to think about it this way, instead of belongs to think
referrs_to or has_fkey_that_refers_to . The model that belongs_to
another model is the one that has the foriegn key so since item has a
note_id columns item belongs_to Note or Item refers_to Note.

Cheers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
This topic is locked and can not be replied to.