# HABTM population from text fields

**URL:** <https://www.ruby-forum.com/t/habtm-population-from-text-fields/132560>\
**Category:** Rails\
**Created:** [March 21, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/habtm-population-from-text-fields/132560 "2008-03-21T13:58:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobeannounced](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tobeannounced](https://www.ruby-forum.com/u/tobeannounced)\
**Post date:** [March 21, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/habtm-population-from-text-fields/132560/1 "2008-03-21T13:58:17Z")

</div>

Hi. I’m having problems wrapping my head around the use of HABTM. I just  
haven’t found a good enough reference for dealing with HABTM-linked  
models, and my business logic is full of them.

In this case, I have a model Initative and a model called Reference for  
associating one or more reference documents to an Initative. Any  
Reference document could also be referenced by any number of Initatives.  
So Initative has objects/columns Initative\_ID and also Ref\_Num:String.  
I have declared HABTM in each model.

I need a straighforward way for a user to enter associated documents  
from the context of an Initative record form. I would think that an  
editable dropdown would be one way, which would allow the user to select  
a Reference already in the database or allow them to enter a new one. I  
also would settle for some version of a text field or text area with a  
“Add Reference” button. I would even consider a popup form with some  
version of these controls.

Being that all of the HABTM references I have seen seem to be really  
checkbox-focused, I haven’t really seen anything that gives me a good  
idea how to go about this. I have done similar things in Access and  
Notes, but can’t quite figure out the HABTM syntax well enough to do it  
in Rails.

Help?

---

<div class="post-metadata">

**Author:** ![tobeannounced](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tobeannounced](https://www.ruby-forum.com/u/tobeannounced)\
**Post date:** [March 21, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/habtm-population-from-text-fields/132560/2 "2008-03-21T17:08:47Z")

</div>

Hello,

Start by reading the following articles:  
[http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off](http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off)  
[http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality](http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality)

You cannot have a many-to-many relationship between Initiative and  
Reference if you put your reference foreign key into your Initiative  
table !

Regards,

Stijn  
[http://www.xaop.com](http://www.xaop.com)

---

<div class="post-metadata">

**Author:** ![tobeannounced](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tobeannounced](https://www.ruby-forum.com/u/tobeannounced)\
**Post date:** [March 21, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/habtm-population-from-text-fields/132560/3 "2008-03-21T21:45:27Z")

</div>

Oops, that was actually just a massive typo on my part.

The Reference model has the field called Reference\_ID (:integer) and  
Ref\_Num (:string). The join table initatives\_references just has  
Reference\_ID, Initative\_ID.

Thanks.

Stijn P. wrote:

> Hello,
> 
> Start by reading the following articles:  
> [has\_many :through - Many-to-many Dance-off!](http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off)  
> [has\_many :through - Basic Rails association cardinality](http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality)
> 
> You cannot have a many-to-many relationship between Initiative and  
> Reference if you put your reference foreign key into your Initiative  
> table !
> 
> Regards,
> 
> Stijn  
> [http://www.xaop.com](http://www.xaop.com)
