I understand HABTM relationships in Rails, but I can''t figure out for the life of me how to actually use them. For example, if you have (as I do) two tables: "people" and "projects," then you need a third table, "people_projects," to act as intermediary. You also need to declare "has_and_belongs_to_many" in each of the two models. Now, having done all that, how would you actually create an association, using a form? If I want to put "Person A" on "Project B," then you would need Person A''s Person.id, and Project B''s Project.id, right? But which model do you update with the form? Do you send it to Person or Project? And when you''re writing the form fields, how do you set up the data to send? For an example, here''s a series of hidden fields with submit buttons: <% for p in @people -%> <div class="teammate"> <% form_tag :action => ''update'', :id => @id do -%> <%= hidden_field ''project'', p.id %> <%= submit_tag p.family_name %> <% end %> </div> <% end -%> @people is the Person model with find(:all). @id is the project id whose page we''re on. ''update'' is just the standard Rails scaffold update method, moved into another controller and adjusted. It works for every other kind of edit on the page. I don''t know which data to include with the hidden field. Right now I have the Project model and I''m trying to send the id of the Person whose name I am displaying in the submit button, so that when you click on the button, that person is associated with the project whose page we''re currently on. It''s not working. It tells me I have an undefined method, but I don''''t know what it''s talking about. Can anybody help? Thanks sean -- 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 5/29/07, Sean Colquhoun <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I understand HABTM relationships in Rails, but I can''t figure out for > the life of me how to actually use them. For example, if you have (as I > do) two tables: "people" and "projects," then you need a third table, > "people_projects," to act as intermediary. You also need to declare > "has_and_belongs_to_many" in each of the two models.I am in the middle of getting to grips with many-many in AR.> > Now, having done all that, how would you actually create an association, > using a form?> If I want to put "Person A" on "Project B," then you would need Person > A''s Person.id, and Project B''s Project.id, right? But which model do you > update with the form? Do you send it to Person or Project?They key thing is that the relationship is two-way, so person.projects << new_project is as valid as project.persons << new_person are both valid. Its the choice that is giving the confusion.> And when you''re writing the form fields, how do you set up the data to > send?Sorry, can''t help you with views, I am Camping on ActiveRecord. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, Sean. Take a look: http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf Atenciosamente, J. Luiz Coe joseluizcoe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://joseluizcoe.com Todas as informações contidas neste e-mail são confidenciais e devem ser usadas apenas pelo seu destinatário. On 5/29/07, Sean Colquhoun <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I understand HABTM relationships in Rails, but I can''t figure out for > the life of me how to actually use them. For example, if you have (as I > do) two tables: "people" and "projects," then you need a third table, > "people_projects," to act as intermediary. You also need to declare > "has_and_belongs_to_many" in each of the two models. > > Now, having done all that, how would you actually create an association, > using a form? > > If I want to put "Person A" on "Project B," then you would need Person > A''s Person.id, and Project B''s Project.id, right? But which model do you > update with the form? Do you send it to Person or Project? > > And when you''re writing the form fields, how do you set up the data to > send? > For an example, here''s a series of hidden fields with submit buttons: > > <% for p in @people -%> > <div class="teammate"> > <% form_tag :action => ''update'', :id => @id do -%> > <%= hidden_field ''project'', p.id %> > <%= submit_tag p.family_name %> > <% end %> > </div> > <% end -%> > > @people is the Person model with find(:all). @id is the project id whose > page we''re on. ''update'' is just the standard Rails scaffold update > method, moved into another controller and adjusted. It works for every > other kind of edit on the page. > > I don''t know which data to include with the hidden field. Right now I > have the Project model and I''m trying to send the id of the Person whose > name I am displaying in the submit button, so that when you click on the > button, that person is associated with the project whose page we''re > currently on. > > It''s not working. It tells me I have an undefined method, but I don''''t > know what it''s talking about. Can anybody help? > > Thanks > sean > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Thanks guys - both of you. I just figured out what this "<<" thing is (adds something onto the end of an array, right?) so I''ll try something with that. In the meantime, I entered some records into people_projects directly in mysql just to see if I could figure out how to get them back out again, and I hit on something: you need to use a find() method for each model, even in the same statement! Like this: <% team = Project.find(@id).people.find(:all) %> <% for t in team -%> <div class="teammate"> <%= button_to t.family_name, :action => "update" %> </div> <% end -%> Am I doing this the "right" way? Or is there a better way that I''m missing? -- 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 -~----------~----~----~----~------~----~------~--~---