On 25 March 2011 22:01, R. K. <[email protected]> wrote: > Colin Law wrote in post #989292: >> On 25 March 2011 20:24, R. K. <[email protected]> wrote: >>> belongs_to :ingredient >>> t.datetime "created_at" >>> t.string "equivalent" >>> t.datetime "created_at" >>> t.datetime "updated_at" >>> end >> >> It is the belongs_to object that should have the foreign key, so >> measurement should have an ingredient_id, not vice versa. Or the >> relationship should be the other way round. >> >> Colin > > I agree which is what is happening in the recipe object I set up. > > Should I be using has_and_belongs_to_many instead since since one > measurement could belong to many different ingredients i.e. a cup of > flour will be used in more than one recipe so that ingredient will be > listed more than once?
You could have an ingredient_measures table that belongs_to a recipe (recipe has_many ingredient_measures). Each ingredient_measures record specifies an ingredient and a measure of that ingredient for the recipe. So you would also need ingredient_measure belongs_to ingredient and belongs_to measure and ingredient and both ingredient and measure has_many ingredient_measures. So for a recipe, @recipe, then @recipe.ingredient_measures will give all the ingredients with their measures. In effect this is a HABTM relationship between ingredient and recipe through ingredient_measures, but there is probably no need to specify it like that unless you need to iterate all ingredients for a particular measure or vice versa, which I guess you probably don't need. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

