HABTM - can grok; can't use. How to use?

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 -%>

<% form_tag :action => 'update', :id => @id do -%> <%= hidden_field 'project', p.id %> <%= submit_tag p.family_name %> <% end %>
<% 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

On 5/29/07, Sean C. [email protected] 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.

Hi, Sean.

Take a look:
http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf

Atenciosamente,

J. Luiz Coe
[email protected]
http://joseluizcoe.com

Todas as informações contidas neste e-mail são confidenciais e devem ser
usadas apenas pelo seu destinatário.

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 -%>


<%= button_to t.family_name, :action => “update” %>

<% end -%>

Am I doing this the “right” way? Or is there a better way that I’m
missing?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs