# Adding habtm through migrations

**URL:** <https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473>\
**Category:** Rails\
**Created:** [June 5, 2006, 10:56am UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473 "2006-06-05T10:56:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 5, 2006, 10:56am UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/1 "2006-06-05T10:56:39Z")

</div>

Hello,

I’m stuck, I’ve got a model Product and would like to add a new model  
called Category. Furthermore, I want to set up a many2many  
relationship between the mentioned models. the problem is that I do  
not know how to set the primary key in the categories\_products table.

this is what I’ve got in my migration file:

create\_table :categories do |t|  
t.column :name, :string

# Other fields here

end

create\_table :categories\_products do |t|  
t.column :category\_id, :integer  
t.column :product\_id, :integer  
#how do I set the primary key to be (category\_id, product\_id)  
end

Cheers

Regards

srdjan

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 5, 2006, 11:14am UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/2 "2006-06-05T11:14:58Z")

</div>

On 6/5/06, Srdjan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> t.column :name, :string
> 
> # Other fields here
> 
> end
> 
> create\_table :categories\_products do |t|  
> t.column :category\_id, :integer  
> t.column :product\_id, :integer  
> #how do I set the primary key to be (category\_id, product\_id)  
> end

> From what you wrote, it doesn’t look like you need to. The HABTM  
> association should work through that table as it is.

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 5, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/3 "2006-06-05T12:31:36Z")

</div>

hi,

> > From what you wrote, it doesn’t look like you need to. The HABTM  
> > association should work through that table as it is.

> -Alder

it does work, sort of. Whenever I reference a category through the  
relationship I get it as a readonly object.  
e.g.

p = Product.find :first  
c = p.categories.first #c is readonly and it cannot be modified

This problem goes away when one sets the primary key as  
_primary key (category\_id, product\_id)_  
through SQL code.

I hoped that there would be a _rails_ way to do the primary key setting.

cheers

srdjan

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 5, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/4 "2006-06-05T17:52:26Z")

</div>

> add\_index(:categories\_products, [:category\_id, :product\_id], :unique =\>  
> true)
> 
> [http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements](http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements)
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

cheers mate, it works perfect now

srdjan

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 5, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/5 "2006-06-05T17:04:20Z")

</div>

Srdjan M. wrote:

> hi,
> 
> > > From what you wrote, it doesn’t look like you need to. The HABTM  
> > > association should work through that table as it is.
> 
> > -Alder
> 
> it does work, sort of. Whenever I reference a category through the  
> relationship I get it as a readonly object.  
> e.g.
> 
> p = Product.find :first  
> c = p.categories.first #c is readonly and it cannot be modified
> 
> This problem goes away when one sets the primary key as  
> _primary key (category\_id, product\_id)_  
> through SQL code.
> 
> I hoped that there would be a _rails_ way to do the primary key setting.

I think what’s going on is that your migration is adding an “id” field  
as the primary key. That’s the default behavior of the create\_table  
method. Look at your table’s schema in an SQL tool to see what’s going  
on there. To disable generation of an “id” field, add a “:id =\> false”  
option to create\_table.

[http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements/create\_table](http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements/create_table)

If for some reason you still need to set the primary key to the  
combination of the foreign keys, the migration method is:

add\_index(:categories\_products, [:category\_id, :product\_id], :unique =\>  
true)

[http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements](http://rails.outertrack.com/module/ActiveRecord%3A%3AConnectionAdapters%3A%3ASchemaStatements)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Srdjan\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@Srdjan\_M](https://www.ruby-forum.com/u/Srdjan_M)\
**Post date:** [June 8, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/adding-habtm-through-migrations/62473/6 "2006-06-08T17:09:33Z")

</div>

Here’s a super-newbie question… How do I go about adding child  
records?

I have a table called ‘accounts’ to which I want to add invoices (an  
account can have many invoices). I have the two models (accounts and  
invoices) all set up.

On the account web page, I have a button\_to link to an action called  
‘add\_invoice’. I added a def for ‘add\_invoice’ in account.rb, but I  
can’t figure out what to put inside the def. Is another controller  
needed (invoice.rb)? Can I do it from within account.rb?

I’m trying to wrap my head around this concept - can anyone help?

TomT
