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