Forum: Ruby on Rails categories/recipes & books/descriptions - has_many vs has_on

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.
55f28e9c77b35b1539af6be60986b0e4?d=identicon&s=25 Philip Rhoades (Guest)
on 2006-01-18 18:03
(Received via mailing list)
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 Rhoades

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:  phil@pricom.com.au
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-18 20:34
(Received via mailing list)
On 1/18/06, Philip Rhoades <phil@pricom.com.au> 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.
55f28e9c77b35b1539af6be60986b0e4?d=identicon&s=25 Philip Rhoades (Guest)
on 2006-01-19 13:31
(Received via mailing list)
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 Bilkovich 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 Rhoades

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:  phil@pricom.com.au
This topic is locked and can not be replied to.