# Newbie relationship confusion

**URL:** <https://www.ruby-forum.com/t/newbie-relationship-confusion/111344>\
**Category:** Rails\
**Created:** [August 6, 2007, 11:33am UTC](https://www.ruby-forum.com/t/newbie-relationship-confusion/111344 "2007-08-06T11:33:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [August 6, 2007, 11:33am UTC](https://www.ruby-forum.com/t/newbie-relationship-confusion/111344/1 "2007-08-06T11:33:39Z")

</div>

Very new to ruby on rails here. I have a background in C and Java  
programming with an RDBMS (mostly Oracle).

I wanted to learn RoR and to do this, I wanted to write a menu planner  
application. The first stage of this is an individual recipe planner.  
I know this has been done before in tutorials but I wanted to be a  
little more sophisticated with it. I’m getting bogged down though in  
relationships and how they are represented in input forms. I’ll  
describe my ERD:

Recipe-||------||||------||-UnitOfMeasure

RecipeLine\>||-------||-Ingredient

I hope that makes sense, I had to include RecipeLine more than once to  
avoid going vertical in a proportional font which wouldn’t have  
worked.

A single Recipe has multiple RecipeLines. A RecipeLine has a single  
ingredient and a single UnitOfMeasure. Each UnitOfMeasure could appear  
on multiple RecipeLines, and each Ingredient could appear on multiple  
RecipeLines.

The reason for splitting out Ingredient from RecipeLine is that when  
planning a shopping list, you only want to purchase sufficient base  
ingredients, regardless of how and in what quantities they’re used in  
each days meal.

My migrations look like this (only for RecipeLines, the others are  
straight forward I think)

class CreateRecipeLines \< ActiveRecord::Migration  
def self.up  
create\_table :recipe\_lines do |t|  
t.column “quantitiy” , :float  
t.column :recipe\_id ,:integer  
t.column :ingredient\_id ,:integer  
t.column :unit\_of\_measure\_id ,:integer  
end  
end

def self.down  
drop\_table :recipe\_lines  
end  
end

And the relationships are defined as

class Recipe \< ActiveRecord::Base  
has\_many :recipelines  
end

class UnitOfMeasure \< ActiveRecord::Base  
has\_many :recipelines  
end

class Ingredient \< ActiveRecord::Base  
has\_many :recipelines  
end

class RecipeLine \< ActiveRecord::Base  
belongs\_to :recipe  
belongs\_to :unitofmeasure  
belongs\_to :ingredient  
end

I think they’re correctly defining the relationships.

I can create and destroy individual rows in each table through the  
scaffolding code, but I’m really struggling to work out how you link  
this lot together in a functional page.

Sorry for the long post people. I’m probably just needing something to  
click here…

Gary

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [August 6, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/newbie-relationship-confusion/111344/2 "2007-08-06T14:26:08Z")

</div>

On 8/6/07, Gary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ingredient and a single UnitOfMeasure. Each UnitOfMeasure could appear
> 
> def self.down  
> class UnitOfMeasure \< ActiveRecord::Base  
> belongs\_to :ingredient  
> end
> 
> I think they’re correctly defining the relationships.
> 
> I can create and destroy individual rows in each table through the  
> scaffolding code, but I’m really struggling to work out how you link  
> this lot together in a functional page.
> 
> Sorry for the long post people. I’m probably just needing something to  
> click here…

Try clicking on this then:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000506](http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000506)

That should at least help you get going.

Cheers,  
David

ps - scaffolding doesn’t do relationships - you have to add that on your  
own.
