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

**URL:** <https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369>\
**Category:** Rails\
**Created:** [May 29, 2007, 11:33am UTC](https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369 "2007-05-29T11:33:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [May 29, 2007, 11:33am UTC](https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369/1 "2007-05-29T11:33:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [May 29, 2007, 12:34pm UTC](https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369/2 "2007-05-29T12:34:33Z")

</div>

On 5/29/07, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [May 29, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369/3 "2007-05-29T13:03:37Z")

</div>

Hi, Sean.

Take a look:  
[http://jrhicks.net/Projects/rails/has\_many\_and\_belongs\_to\_many.pdf](http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

Atenciosamente,

J. Luiz Coe  
removed\_email\_address@domain.invalid

> **[Tecnologia, carreira, liderança](https://joseluizcoe.com/)**
>
> Esse blog é sobre tudo que eu estou estudando, vivendo, aprendendo e não consigo guardar só pra mim. Me siga nas redes, vamos conversar!

Todas as informaÃ§Ãµes contidas neste e-mail sÃ£o confidenciais e devem ser  
usadas apenas pelo seu destinatÃ¡rio.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [May 30, 2007, 6:35am UTC](https://www.ruby-forum.com/t/habtm-can-grok-cant-use-how-to-use/105369/4 "2007-05-30T06:35:46Z")

</div>

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?
