Beginner: has_many relationship Active Record Associations

Hi There,

I got stuck in a Problem with Active Record Associations.

There are some Meals and each meal has many sides.

But it's only this single direction - so a side don't need to belong to the meal. I don't wanna search: "Give me the meals where side is ..."

I'll show you the problem with an example:

Meal 1:   Side_8   Side_10   Side_11

Meal 2:   Side_2   Side_8   Side_10

So one Side could be associated with many meals.

But I'm not shure how to create this with active Record.

I thought it should be enough to do this:

class Meal < ActiveRecord::Base   has_many :sides end

class Side < ActiveRecord::Base end

But the meal_id is saved at the Side - so a side will only belong to the last associated Meal - am I right?

Thanks allot for your help Chris

You want a habtm relationship, or a :through a join model (named like ‘Siding’ to demonstrate its intention to link Side to something). Consider also that i may order two sides of the same item with one main… you need a join table to model that.

Also have a look at the Rails Guide on ActiveRecord Associations. That may help to clarify things.

Colin