Hi I''m porting an existing project to fit the REST philosofie. I have 3 resources that are nested: users, companies and roles. A user has and belongs to multiple companies, and depening on the company, a role will be assigned. So I have an association table: users_companies with the following fields: user_id, company_id, role_id. But how do I assign a user to a company with a role the REST way? Normally you would create a new controller, but I would like to do the assignments in the user form. Anyone that has some experience on this? Thank you in advance. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Michael Rigart wrote:> Hi > > I''m porting an existing project to fit the REST philosofie. I have 3 > resources that are nested: users, companies and roles. A user has and > belongs to multiple companies, and depening on the company, a role will > be assigned. > > So I have an association table: users_companies with the following > fields: user_id, company_id, role_id. But how do I assign a user to a > company with a role the REST way? > > Normally you would create a new controller, but I would like to do the > assignments in the user form. > > Anyone that has some experience on this? > > Thank you in advance.Sorry for adding this, but the association should also made possible on the company form. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Jeff Cohen wrote:> On Jul 28, 6:23�am, Michael Rigart <rails-mailing-l...@andreas-s.net> > wrote: >> Michael Rigart wrote: > >> >> Sorry for adding this, but the association should also made possible on >> the company form. >> > > I thought I had an answer until I saw this addendum :-) > > Can you explain a little more what the company form is trying to do? > > JeffSorry Jeff if I wasn''t clear enough. The company and user form are basically used to create and / or edit a company or user. Under the user details, there will be another "section" where the administrator can assign the companies to the user he is creating /editing with a sertain role. On the other hand, when he is creating a new company, under the company details, there will be another "section" where the admin can assign users to the company he is creating / editing with a sertain role. So its a litle complex and I can''t see how to do this as restful as possible. Thank you for your intereset. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Anyone who has an idea this one? -- 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-/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 Jul 28, 9:51 am, Michael Rigart <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Sorry Jeff if I wasn''t clear enough. > The company and user form are basically used to create and / or edit a > company or user. > > Under the user details, there will be another "section" where the > administrator can assign the companies to the user he is creating > /editing with a sertain role. > > On the other hand, when he is creating a new company, under the company > details, there will be another "section" where the admin can assign > users to the company he is creating / editing with a sertain role. > > So its a litle complex and I can''t see how to do this as restful as > possible.It sounds like you need to update multiple kinds of resources from one form, which unfortunately Rails doesn''t handle very well yet (there are some proposed changes in edge that might help this, though). I''d recommend posting to the controller that seems to represent the "main" resource (the user or company, as needed). I''m imagining you''ve got some checkboxes for users (or companies) that will come along for the ride. That being the case, I would suggest that your create action (say, for users) pull out the company list from the params hash first: # remove company IDs from the params hash # and keep the array of company IDs for later use # You might not need this step depending on # how your checkbox names are setup company_ids = params[:user].delete :company Now the params hash has just the user info remaining in it: user = User.build(params[:user]) Now you can associate the user to the companies represented by the ids in one fell swoop: user.company_ids = company_ids Then save everything: user.save # should save the relationships as well, I believe Although it might seem "non-restful" to be assigning the company relationships from within the create action, I can''t think of a better way to do it, and to me it actually seems reasonable. If the UI is specifying that creating a user includes assigning some associations, then I think it''s legit to do this all within the create action. What do you think? Jeff --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---