Okay, so I have a many-to-many relationship between users and groups set up in my application, using a join table (users_groups). I am able to reference one from the other using this relaionship. However, I''m not exactly sure how to relate a User to a Group in the app, other than hard-coding some SQL to insert a new relationship. What''s the proper way to do 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 -~----------~----~----~----~------~----~------~--~---
You should be able to append instances of one to the collection accessor of the other. So for instance: fred = User.find_by_name("fred") godlike_beings = Group.find_by_name("godlike beings") godlike_beings.users << fred godlike_beings.save # or equivalently: fred.groups << godlike_beings fred.save HTH, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of DVG Sent: Tuesday, May 13, 2008 8:10 AM To: Ruby on Rails: Talk Subject: [Rails] Using a Many-to-Many Relationship Okay, so I have a many-to-many relationship between users and groups set up in my application, using a join table (users_groups). I am able to reference one from the other using this relaionship. However, I''m not exactly sure how to relate a User to a Group in the app, other than hard-coding some SQL to insert a new relationship. What''s the proper way to do 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The one caveat to Roy''s response is that both the User and the Group must exist. It''s tempting to use that same line of coding when you''re creating the end points and the join table at the same time but it doesn''t work. On May 13, 12:58 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> You should be able to append instances of one to the collection accessor > of the other. So for instance: > > fred = User.find_by_name("fred") > godlike_beings = Group.find_by_name("godlike beings") > > godlike_beings.users << fred > godlike_beings.save > > # or equivalently: > > fred.groups << godlike_beings > fred.save > > HTH, > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of DVG > Sent: Tuesday, May 13, 2008 8:10 AM > To: Ruby on Rails: Talk > Subject: [Rails] Using a Many-to-Many Relationship > > Okay, so I have a many-to-many relationship between users and groups set > up in my application, using a join table (users_groups). I am able to > reference one from the other using this relaionship. However, I''m not > exactly sure how to relate a User to a Group in the app, other than > hard-coding some SQL to insert a new relationship. What''s the proper way > to do 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 -~----------~----~----~----~------~----~------~--~---
True dat. I''ve found that to be a bit challenging myself (as evidenced by my questions over the last few weeks). FWIW, I took the fields_for + virtual attribute approach Ryan Bates explains in these railscasts: http://railscasts.com/episodes/73 http://railscasts.com/episodes/74 Took me a while to get my head around that, but it seems to work... -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of AndyV Sent: Tuesday, May 13, 2008 5:36 PM To: Ruby on Rails: Talk Subject: [Rails] Re: Using a Many-to-Many Relationship The one caveat to Roy''s response is that both the User and the Group must exist. It''s tempting to use that same line of coding when you''re creating the end points and the join table at the same time but it doesn''t work. On May 13, 12:58 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> You should be able to append instances of one to the collection > accessor of the other. So for instance: > > fred = User.find_by_name("fred") > godlike_beings = Group.find_by_name("godlike beings") > > godlike_beings.users << fred > godlike_beings.save > > # or equivalently: > > fred.groups << godlike_beings > fred.save > > HTH, > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of DVG > Sent: Tuesday, May 13, 2008 8:10 AM > To: Ruby on Rails: Talk > Subject: [Rails] Using a Many-to-Many Relationship > > Okay, so I have a many-to-many relationship between users and groups > set up in my application, using a join table (users_groups). I am able> to reference one from the other using this relaionship. However, I''m > not exactly sure how to relate a User to a Group in the app, other > than hard-coding some SQL to insert a new relationship. What''s the > proper way to do 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---