Categories/recipes & books/descriptions - has_many vs has_on

People,

In the cookbook eg, categories has_many recipes but in a book eg, book
has_one description - doesn’t that mean that the id of the description
should be the same as the id of the book (instead of having it’s own
“description_id” in the book table?

Thanks,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: [email protected]

On 1/18/06, Philip R. [email protected] wrote:

People,

In the cookbook eg, categories has_many recipes but in a book eg, book
has_one description - doesn’t that mean that the id of the description
should be the same as the id of the book (instead of having it’s own
“description_id” in the book table?

The category → recipes tables look like this:
categories
-id
-other_stuff

recipes
-id
-category_id
-other_stuff

The book → description relationship implies this kind of setup:
books
-id
-title
-other_stuff

descriptions
-id
-book_id
-description_text

class Book
has_one :description #Note the singular, rather than plural.
end

class Description
belongs_to :book
end

@book = Book.find(:first)

@book.description returns the instance of Description that was
associated with that book.

In a typical setup, every ActiveRecord class (Book, Description, etc)
maps to a database table, and that table has an ‘id’ column for the
primary key. Descriptions each have a unique id independent of their
Book.

Wilson,

Thanks for that - but I am still unclear - if there is only one
description per book, isn’t it unnecessary to have a field “book_id” for
descriptions? (ie book id = description id). This is what I would do if
I were managing this manually. I guess it is keep the automatic
scaffolding stuff consistent?

Regards,

Phil.

On Wed, 2006-01-18 at 13:56 -0500, Wilson B. wrote:

categories
-id
end
In a typical setup, every ActiveRecord class (Book, Description, etc)
maps to a database table, and that table has an ‘id’ column for the
primary key. Descriptions each have a unique id independent of their
Book.


Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: [email protected]