Newbie relationship confusion

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

On 8/6/07, Gary [email protected] 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

That should at least help you get going.

Cheers,
David

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