Philip Rhoades
2006-Jan-18 16:24 UTC
[Rails] categories/recipes & books/descriptions - has_many vs has_one => id question
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
Wilson Bilkovich
2006-Jan-18 18:57 UTC
[Rails] categories/recipes & books/descriptions - has_many vs has_one => id question
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.
Philip Rhoades
2006-Jan-19 11:52 UTC
[Rails] categories/recipes & books/descriptions - has_many vs has_one => id question
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:> 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. >-- 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
Apparently Analagous Threads
- Cookbook recipes eg - ordering categories in the recipe pull-down box
- recipes/categories to books/authors but listing doesn''t work
- Repost: Pluralization of non-noun names
- Scaffolding - disabling Create, Edit, Destroy for some user logins?
- What is this relationship?