# Adding registers in HABTM relationship

**URL:** <https://www.ruby-forum.com/t/adding-registers-in-habtm-relationship/171234>\
**Category:** Rails\
**Created:** [June 29, 2009, 10:47am UTC](https://www.ruby-forum.com/t/adding-registers-in-habtm-relationship/171234 "2009-06-29T10:47:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 29, 2009, 10:47am UTC](https://www.ruby-forum.com/t/adding-registers-in-habtm-relationship/171234/1 "2009-06-29T10:47:03Z")

</div>

Hi!

I have a little problem in a HABTM relationship.  
I have this schema:

expedients \<—\> expedients\_documents \<—\> documents  
expedients(HABTM)docuements \<-\> documetns(HABTM)expedients

From the expedients view, I add a new document, passing as parameter  
the expedients ids (maybe more than one).However, at time that I  
create the document, I cannot put the values in the table  
expedients\_documents.

Should I use nested attributes for this? Im reading about, and i think  
that putting in the documents/new view a fields\_for :expedients must  
complaint this, but I cannot get a correct result.

Thanks, and best regards.

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 29, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/adding-registers-in-habtm-relationship/171234/2 "2009-06-29T16:40:48Z")

</div>

Solved.I write a comment for newbies as me…sorry for the english.

The problem raises from the generation of the join table.This table,  
cannot must have a id attribute as a primary key, instead of this,  
must have a primary key composed by the two foreign keys.In Rails,  
this means a :id =\> false in the migration like this…

class DocumentosExpedientes \< ActiveRecord::Migration  
def self.up  
create\_table :documentos\_expedientes,:id =\> false do |t|  
t.integer :documento\_id  
t.integer :expediente\_id  
t.timestamps  
end  
end

After this, the only thing that I must do is

documento.expedientes\_ids \<\< expedientes #expedientes is a array of  
#Expediente

On 29 jun, 10:46, Jose Ernesto S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [June 29, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/adding-registers-in-habtm-relationship/171234/3 "2009-06-29T17:21:19Z")

</div>

You might as well skip the timestamps as well, as the “HABTM table  
with extra fields” functionality disappeared in 2.3.

–Matt J.

On Jun 29, 10:39 am, Jose Ernesto S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
