Jump to content

The ultimate community for Ruby on Rails developers.


database design help required

model design noob

  • Please log in to reply
4 replies to this topic

#1 moro



  • Members
  • 6 posts

Posted 14 February 2014 - 11:11 AM

Hello world,


sorry this is my first post here, and my first real rails project, so I hope I'm not bothering you with too easy questions;


I've decided to develop an application to manage my receipes (I'm from Italy and - yes stereotypes and clichè - I love cooking).

I'm still planning the database (so "drawing" the models), but I'm stuck:


I was planning to have mainly these two objects:









and here comes my problem: I want every receips to have a link to the ingredients. How can i dynamically add or remove :ingredient_id strings to the Receipe object?


Moreover I would like to add an amount for every ingredient for every recipe. How can I do that?




Sorry, I don't know if I expressed clearly enough...


Thank you for your help



#2 Bharat Soni

Bharat Soni


  • Members
  • 55 posts
  • LocationAhmedabad

Posted 14 February 2014 - 12:16 PM



   Firstly you must have read the tutorials for creating application in ruby on rails. 


Here are some url list where you can learn to code with ruby on rails.


1. http://guides.rubyonrails.org/



Fun with Ruby on Rails

#3 Bharat Soni

Bharat Soni


  • Members
  • 55 posts
  • LocationAhmedabad

Posted 14 February 2014 - 12:27 PM   Best Answer

Here your answer


Firstly create two models like

rails g model recipe name:string description:string

rails g model  ingredient name:string description:string cal_per_gram:integer amount:decimal recipe_id:integer

it will create two migration for you then run that command

rake db:migrate  

Now it will create two model with name recipe.rb and ingredient.ib so inside it you can define the relations like that


in recipe.rb write inside class

has_one :ingredient, :class_name=>"Ingredient", :foreign_key=>"recipe_id", :dependent=>:destroy

in ingredient.rb write inside class

belongs_to :recipe

  • james likes this

Fun with Ruby on Rails

#4 moro



  • Members
  • 6 posts

Posted 12 March 2014 - 09:40 AM



After reading pages and books I'm now able to understand well your reply..

Thank you!

#5 Jemagee



  • Members
  • 62 posts

Posted 29 March 2014 - 06:37 PM

I have to say that I disagree that this is answered


There is a 1:1 relationship between ingredient and recipe - what if you use the same ingredient in many recipes?


I've thought about a recipe database - and it needs a cross join table






A Recipe has many ingredients through IngredientRecipe

An Ingreident belongs to many Recipes through IngredientRecipe


As for the dependent destroy - you don't want to destroy an ingredient when you destroy a recipe because you might use that ingredient in OTHER recipes (or you might want to present ingredient data in other places) - When you destroy a recipe the corresponding IngredientRecipe columns (this is a cross join so has no primary_id column) should be destroyed.


I'm not sure why you would destroy an ingredient - but if you did so - you probably should destroy all related recipes BECAUSE if you destroy one ingredient - none of those recipes are valid any more.


The original answer does not take into account dynamic growth of recipes and various ingredients


And you should consider ingredient 'groups'




If you're mkaing a cake - you can use numerous sources of sugar (white sugar, brown sugar, maple syrup, etcc) or different sources of fat (butter, oil, crisco, etc...)


(Yes - I've thought about a recipe database quite a bit)

Also tagged with one or more of these keywords: model, design, noob

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users