# Categories/recipes & books/descriptions - has\_many vs has\_on

**URL:** <https://www.ruby-forum.com/t/categories-recipes-books-descriptions-has-many-vs-has-on/52750>\
**Category:** Rails\
**Created:** [January 18, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/categories-recipes-books-descriptions-has-many-vs-has-on/52750 "2006-01-18T18:03:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 18, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/categories-recipes-books-descriptions-has-many-vs-has-on/52750/1 "2006-01-18T18:03:28Z")

</div>

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: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 18, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/categories-recipes-books-descriptions-has-many-vs-has-on/52750/2 "2006-01-18T20:34:34Z")

</div>

On 1/18/06, Philip R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 19, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/categories-recipes-books-descriptions-has-many-vs-has-on/52750/3 "2006-01-19T13:31:37Z")

</div>

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: removed\_email\_address@domain.invalid
