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 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.
Michael Pavling
2011-Oct-18 12:09 UTC
Re: Beginner: has_many relationship Active Record Associations
On 18 Oct 2011 12:56, "Chris" <christian.freisen-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 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 ..." > > So one Side could be associated with many meals. > > But I''m not shure how to create this with active Record. >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. -- 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.
Colin Law
2011-Oct-18 12:12 UTC
Re: Beginner: has_many relationship Active Record Associations
On 18 October 2011 13:09, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 18 Oct 2011 12:56, "Chris" <christian.freisen-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> 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 ..." >> >> So one Side could be associated with many meals. >> >> But I''m not shure how to create this with active Record. >> > > 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 -- 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.