Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

cookbook problem

I've been asked to design and build a cookbook site in RoR. If I were building it in php I would compose a recipe object containing an ingredient list and instruction set. But how would I accomplish this in Rails? A recipe has a list of ingredients and a description of how to combine those ingredients, and some recipes have multiple components. I haven't yet gotten into how to use related tables, so if there is a lesson I should refer to, I'd appreciate it if you'd point it out to me. Thanks!

2 Answers

Sandra Weber
Sandra Weber
1,531 Points

Hi Osgaldor,

I think related tables are exactly what you need! In a Rails application it's very common to have multiple tables with defined relationships between them. Rails calls these relationships "associations". The intermediate project Building Social Features in Ruby on Rails introduces associations and how to use them to set up relationships between your models. After that, the Rails guide to associations should be helpful if you want to get more in-depth.

Hope this helps!

Thank you! Perfect!

Tho my concern really is more about how to avoid having to create separate tables/models for ingredient lists and instruction sets, those items won't apply to any other recipe but the one to which they are associated. How would I build a recipe object that encapsulates the ingredients (a list of ingredient items) and an instruction set (text blob).

James Barnett
James Barnett
39,199 Points

Osgaldor Storm - It seems like you need to give some thought to your database/model design.

Sandra Weber
Sandra Weber
1,531 Points

Osgaldor,

What you're referring to (storing a list of things in one database field as opposed to in their own table) is called a de-normalized database. There are arguments for and against database normalization, but what you want to do is definitely possible in Rails using serialization.

Keep in mind that Rails provides a lot of help for associations (form builders & handling of form data, saving associated objects along with the parent object, powerful scoped database queries) that you won't get if you decide to go with a serialized field.