I have an Ingredient which has a Measurement. class Ingredient < ActiveRecord::Base has_one :measurement belongs_to :recipe accepts_nested_attributes_for :measurement end class Measurement < ActiveRecord::Base belongs_to :ingredient end The schemas appear right: create_table "ingredients", :force => true do |t| t.integer "measurement_id" t.integer "recipe_id" t.string "quantity" t.string "food" t.datetime "created_at" t.datetime "updated_at" end create_table "measurements", :force => true do |t| t.string "size" t.float "quantity" t.string "abbreviation" t.string "measurement" t.string "measurement_type" t.string "equivalent" t.datetime "created_at" t.datetime "updated_at" end But I''m still getting this error preventing me from doing an ingredient.measurement.abbreviation, but ingredient.measurement_id works: ActiveRecord::StatementInvalid in Recipes#show Showing /Users/mkidd/Sites/cookbook/app/views/recipes/show.html.erb where line #14 raised: SQLite3::SQLException: no such column: measurements.ingredient_id: SELECT "measurements".* FROM "measurements" WHERE ("measurements".ingredient_id = 1) LIMIT 1 Extracted source (around line #14): 11: <% for ingredient in @recipe.ingredients%> 12: <li> 13: <%= ingredient.quantity %> 14: <%= ingredient.measurement %> 15: <%= ingredient.food %> 16: </li> 17: <% end %> -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 March 2011 20:24, R. K. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have an Ingredient which has a Measurement. > > class Ingredient < ActiveRecord::Base > has_one :measurement > belongs_to :recipe > > accepts_nested_attributes_for :measurement > end > > class Measurement < ActiveRecord::Base > belongs_to :ingredient > end > > > The schemas appear right: > > create_table "ingredients", :force => true do |t| > t.integer "measurement_id" > t.integer "recipe_id" > t.string "quantity" > t.string "food" > t.datetime "created_at" > t.datetime "updated_at" > end > > > create_table "measurements", :force => true do |t| > t.string "size" > t.float "quantity" > t.string "abbreviation" > t.string "measurement" > t.string "measurement_type" > t.string "equivalent" > t.datetime "created_at" > t.datetime "updated_at" > endIt 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #989292:> On 25 March 2011 20:24, R. K. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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. > > ColinI 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? -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 March 2011 22:01, R. K. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #989292: >> On 25 March 2011 20:24, R. K. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.