I''m very new at rails and wondering if ActiveRecord supports (many-to- many)-to many relationships. For example: I have recipes, ingredients, and cookingmethods. Recipes have many ingredients, and ingredients are in many recipes. For each ingredient in a recipe, there is a different cooking method. For example, potatoes in a salad are diced, but potatoes in potato chips are sliced. Example database tables: recipes: id, name recipes_ingredients: id, recipe_id, ingredient_id ingredients: ingredient_id, name cookingmethods: id, name recipes_ingredients_cookingmethods: recipe_ingredient_id, cookingmethod_id I looked at through associations, but those seemed to deal only with many-to-many relationships. Thanks, Gerry --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m, looking at something similar, try this (http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/ b4e27651fb149e48?lnk=igtc) and let me know how you get on? I''d be interested in seeing your model and any extra finder methods you add... Feel free to gTalk me J On Feb 23, 10:49 am, "mastica" <mast...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m very new at rails and wondering if ActiveRecord supports (many-to- > many)-to many relationships. > > For example: I have recipes, ingredients, and cookingmethods. Recipes > have many ingredients, and ingredients are in many recipes. For each > ingredient in a recipe, there is a different cooking method. For > example, potatoes in a salad are diced, but potatoes in potato chips > are sliced. Example database tables: > > recipes: id, name > recipes_ingredients: id, recipe_id, ingredient_id > ingredients: ingredient_id, name > cookingmethods: id, name > recipes_ingredients_cookingmethods: recipe_ingredient_id, > cookingmethod_id > > I looked at through associations, but those seemed to deal only with > many-to-many relationships. > > Thanks, > > Gerry--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-23 14:05 UTC
Re: (Many-to-many)-to many
To do this, you have your ingredients, recipes and cooking_methods tables, as well as a join table... let''s call this recipe_ingredients. This would have foreign key columns, one for each of the other tables. Recipe has_many :recipe_ingredients has_many :ingredients, :through => :recipe_ingredients has_many :cooking_methods, :through => :recipe_ingredients Ingredient has_many :recipe_ingredients has_many :recipes, :through => :recipe_ingredients has_many :cooking_methods, :through => :recipe_ingredients CookingMethod has_many :recipe_ingredients has_many :ingredients, :through => :recipe_ingredients has_many :recipes, :through => :recipe_ingredients RecipeIngredient belongs_to :recipe belongs_to :ingredient belongs_to :cooking_method There. It''s quite easy really. You just have to break down three-way relationships into two way ones, and that''ll solve any (database) relationship problems. Hope this helps, -Nathan On 23/02/07, mastica <mastica-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > many)-to many relationships. > > For example: I have recipes, ingredients, and cookingmethods. Recipes > have many ingredients, and ingredients are in many recipes. For each > ingredient in a recipe, there is a different cooking method. For > example, potatoes in a salad are diced, but potatoes in potato chips > are sliced. Example database tables: > > recipes: id, name > recipes_ingredients: id, recipe_id, ingredient_id > ingredients: ingredient_id, name > cookingmethods: id, name > recipes_ingredients_cookingmethods: recipe_ingredient_id, > cookingmethod_id > > I looked at through associations, but those seemed to deal only with > many-to-many relationships. > > Thanks, > > Gerry > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Feb-23 14:06 UTC
Re: (Many-to-many)-to many
*this would have three foreign key columns, one for each of the other tables. On 23/02/07, njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > To do this, you have your ingredients, recipes and cooking_methods tables, > as well as a join table... let''s call this recipe_ingredients. This would > have foreign key columns, one for each of the other tables. > > Recipe > has_many :recipe_ingredients > has_many :ingredients, :through => :recipe_ingredients > has_many :cooking_methods, :through => :recipe_ingredients > > Ingredient > has_many :recipe_ingredients > has_many :recipes, :through => :recipe_ingredients > has_many :cooking_methods, :through => :recipe_ingredients > > CookingMethod > has_many :recipe_ingredients > has_many :ingredients, :through => :recipe_ingredients > has_many :recipes, :through => :recipe_ingredients > > RecipeIngredient > belongs_to :recipe > belongs_to :ingredient > belongs_to :cooking_method > > There. It''s quite easy really. You just have to break down three-way > relationships into two way ones, and that''ll solve any (database) > relationship problems. > > Hope this helps, > -Nathan > > > On 23/02/07, mastica <mastica-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > > many)-to many relationships. > > > > For example: I have recipes, ingredients, and cookingmethods. Recipes > > have many ingredients, and ingredients are in many recipes. For each > > ingredient in a recipe, there is a different cooking method. For > > example, potatoes in a salad are diced, but potatoes in potato chips > > are sliced. Example database tables: > > > > recipes: id, name > > recipes_ingredients: id, recipe_id, ingredient_id > > ingredients: ingredient_id, name > > cookingmethods: id, name > > recipes_ingredients_cookingmethods: recipe_ingredient_id, > > cookingmethod_id > > > > I looked at through associations, but those seemed to deal only with > > many-to-many relationships. > > > > Thanks, > > > > Gerry > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''d be interested to see how the and controllers and views should be constructed for this model J On Feb 23, 2:06 pm, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> *this would have three foreign key columns, one for each of the other > tables. > > On 23/02/07, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > To do this, you have your ingredients, recipes and cooking_methods tables, > > as well as a join table... let''s call this recipe_ingredients. This would > > have foreign key columns, one for each of the other tables. > > > Recipe > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > Ingredient > > has_many :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > CookingMethod > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > > RecipeIngredient > > belongs_to :recipe > > belongs_to :ingredient > > belongs_to :cooking_method > > > There. It''s quite easy really. You just have to break down three-way > > relationships into two way ones, and that''ll solve any (database) > > relationship problems. > > > Hope this helps, > > -Nathan > > > On 23/02/07, mastica <mast...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > > > many)-to many relationships. > > > > For example: I have recipes, ingredients, and cookingmethods. Recipes > > > have many ingredients, and ingredients are in many recipes. For each > > > ingredient in a recipe, there is a different cooking method. For > > > example, potatoes in a salad are diced, but potatoes in potato chips > > > are sliced. Example database tables: > > > > recipes: id, name > > > recipes_ingredients: id, recipe_id, ingredient_id > > > ingredients: ingredient_id, name > > > cookingmethods: id, name > > > recipes_ingredients_cookingmethods: recipe_ingredient_id, > > > cookingmethod_id > > > > I looked at through associations, but those seemed to deal only with > > > many-to-many relationships. > > > > Thanks, > > > > Gerry--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks. I''ll try that out. I''ve never gotten such a quick answer from posting a programming question on any forum before. Gerry On Feb 23, 9:06 am, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> *this would have three foreign key columns, one for each of the other > tables. > > On 23/02/07, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > To do this, you have your ingredients, recipes and cooking_methods tables, > > as well as a join table... let''s call this recipe_ingredients. This would > > have foreign key columns, one for each of the other tables. > > > Recipe > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > Ingredient > > has_many :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > CookingMethod > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > > RecipeIngredient > > belongs_to :recipe > > belongs_to :ingredient > > belongs_to :cooking_method > > > There. It''s quite easy really. You just have to break down three-way > > relationships into two way ones, and that''ll solve any (database) > > relationship problems. > > > Hope this helps, > > -Nathan > > > On 23/02/07, mastica <mast...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > > > many)-to many relationships. > > > > For example: I have recipes, ingredients, and cookingmethods. Recipes > > > have many ingredients, and ingredients are in many recipes. For each > > > ingredient in a recipe, there is a different cooking method. For > > > example, potatoes in a salad are diced, but potatoes in potato chips > > > are sliced. Example database tables: > > > > recipes: id, name > > > recipes_ingredients: id, recipe_id, ingredient_id > > > ingredients: ingredient_id, name > > > cookingmethods: id, name > > > recipes_ingredients_cookingmethods: recipe_ingredient_id, > > > cookingmethod_id > > > > I looked at through associations, but those seemed to deal only with > > > many-to-many relationships. > > > > Thanks, > > > > Gerry--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
My inner database normalization conscience has some misgivings about this schema. Suppose the number of cooking methods is large compared to the number of recipe/ingredients. So potatoes in potato chips can be sliced, diced, grated... Then I''ll end up duplicating the recipe/ ingredients foreign keys a lot in the join table. Suppose further that people can comment on each recipe/ingredient/ cooking method combination. I would have to add a "comments" column to recipes_ingredients. Then for each comment, I''ll have to duplicate the keys for recipes/ingredients/cookingmethod...this is clearly unnormalized. So I think my original schema gives me more flexibility in the long run. Any way I can get rails to work with the original schema (see first post in thread)? Thanks for your help. Gerry On Feb 23, 9:05 am, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> To do this, you have your ingredients, recipes and cooking_methods tables, > as well as a join table... let''s call this recipe_ingredients. This would > have foreign key columns, one for each of the other tables. > > Recipe > has_many :recipe_ingredients > has_many :ingredients, :through => :recipe_ingredients > has_many :cooking_methods, :through => :recipe_ingredients > > Ingredient > has_many :recipe_ingredients > has_many :recipes, :through => :recipe_ingredients > has_many :cooking_methods, :through => :recipe_ingredients > > CookingMethod > has_many :recipe_ingredients > has_many :ingredients, :through => :recipe_ingredients > has_many :recipes, :through => :recipe_ingredients > > RecipeIngredient > belongs_to :recipe > belongs_to :ingredient > belongs_to :cooking_method > > There. It''s quite easy really. You just have to break down three-way > relationships into two way ones, and that''ll solve any (database) > relationship problems. > > Hope this helps, > -Nathan > > On 23/02/07, mastica <mast...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > > many)-to many relationships. > > > For example: I have recipes, ingredients, and cookingmethods. Recipes > > have many ingredients, and ingredients are in many recipes. For each > > ingredient in a recipe, there is a different cooking method. For > > example, potatoes in a salad are diced, but potatoes in potato chips > > are sliced. Example database tables: > > > recipes: id, name > > recipes_ingredients: id, recipe_id, ingredient_id > > ingredients: ingredient_id, name > > cookingmethods: id, name > > recipes_ingredients_cookingmethods: recipe_ingredient_id, > > cookingmethod_id > > > I looked at through associations, but those seemed to deal only with > > many-to-many relationships. > > > Thanks, > > > Gerry--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/24/07, mastica <mastica-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > My inner database normalization conscience has some misgivings about > this schema. Suppose the number of cooking methods is large compared > to the number of recipe/ingredients. So potatoes in potato chips can > be sliced, diced, grated... Then I''ll end up duplicating the recipe/ > ingredients foreign keys a lot in the join table. > > Suppose further that people can comment on each recipe/ingredient/ > cooking method combination. I would have to add a "comments" column to > recipes_ingredients. Then for each comment, I''ll have to duplicate the > keys for recipes/ingredients/cookingmethod...this is clearly > unnormalized.normalisation is not necessarily the best design in all cases... but I digress..> So I think my original schema gives me more flexibility in the long > run. Any way I can get rails to work with the original schema (see > first post in thread)?Of course. You need to promote your ingredient/cookingmethod table to a proper first-class model. If you find yourself talking (or thinking) about a join table a lot, it''s usually a sign that it wants to be a model. You''ll also want to read up on has_many :through. Let''s call that model Preparation. You then have: class Ingredient has_many :preparations has_many :cooking_methods, :through=>:preparations has_many :recipes, :through=>:preparations end class Recipe has_and_belongs_to_many :preparations has_many :ingredients, :through=>:preparations end class CookingMethod has_many :preparations has_many :ingredients, :through=>:preparations end class Preparation has_and_belongs_to_many :recipes belongs_to :ingredient belongs_to :cooking_method end 5 tables total, one for each of the models, plus a preparations_recipes table. --max> > Thanks for your help. > > Gerry > > On Feb 23, 9:05 am, njmacin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > To do this, you have your ingredients, recipes and cooking_methods tables, > > as well as a join table... let''s call this recipe_ingredients. This would > > have foreign key columns, one for each of the other tables. > > > > Recipe > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > > Ingredient > > has_many :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > has_many :cooking_methods, :through => :recipe_ingredients > > > > CookingMethod > > has_many :recipe_ingredients > > has_many :ingredients, :through => :recipe_ingredients > > has_many :recipes, :through => :recipe_ingredients > > > > RecipeIngredient > > belongs_to :recipe > > belongs_to :ingredient > > belongs_to :cooking_method > > > > There. It''s quite easy really. You just have to break down three-way > > relationships into two way ones, and that''ll solve any (database) > > relationship problems. > > > > Hope this helps, > > -Nathan > > > > On 23/02/07, mastica <mast...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > I''m very new at rails and wondering if ActiveRecord supports (many-to- > > > many)-to many relationships. > > > > > For example: I have recipes, ingredients, and cookingmethods. Recipes > > > have many ingredients, and ingredients are in many recipes. For each > > > ingredient in a recipe, there is a different cooking method. For > > > example, potatoes in a salad are diced, but potatoes in potato chips > > > are sliced. Example database tables: > > > > > recipes: id, name > > > recipes_ingredients: id, recipe_id, ingredient_id > > > ingredients: ingredient_id, name > > > cookingmethods: id, name > > > recipes_ingredients_cookingmethods: recipe_ingredient_id, > > > cookingmethod_id > > > > > I looked at through associations, but those seemed to deal only with > > > many-to-many relationships. > > > > > Thanks, > > > > > Gerry > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Im getting an error when running an app under fastCGI file ../public_html/../config/../tmp/sessions//ruby_sess.(whatver) not readable app works fine under regular cgi. What would cause this? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---