Belongs_to, has_one, has_many question (again?)


#1

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


#2

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 Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732