# Many To Many's Join table attributes + migrations

**URL:** <https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378>\
**Category:** Rails\
**Created:** [April 10, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378 "2006-04-10T15:19:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lachlan\_Deck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Lachlan\_Deck](https://www.ruby-forum.com/u/Lachlan_Deck)\
**Post date:** [April 10, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378/1 "2006-04-10T15:19:51Z")

</div>

Hi all,

just joined and have only just kicked off with Rails (coming, but not  
departing, from WebObjects development). And so far there seems to be  
a lot of promise except for one thing I can’t figure out as yet –  
the above problem.

Say I have the following conceptual relationships:

```
ITEMS <<--->> CARTS

```

i.e., each item can belong to one or more categories. So really we have

```
ITEMS <<--> ITEMS_CARTS <-->> CARTS

```

where the join table, items\_carts, has the dual primary keys  
{items\_id, carts\_id}.

However, in reality people can and will buy more than one of each  
item and so items\_carts needs an additional attribute: quantity.

My question is how might the above be defined as a migration? In  
WebObjects when modelling the above entities (akin to migrations) you  
simply set the outer entities to propagate their primary keys.

I’ve searched google for quite some time but seem to keep finding  
either objections to allowing for multiple primary keys (even though  
many-to-many’s are common-place) or mention of work-a-rounds in  
progress.

Any ideas?  
I’ve seen the following article but it doesn’t go into enough detail  
and straight away bypasses the abstract migration tool using instead  
database specific sql (which suggests that the built in features may  
not exist for this yet): [jrhicks.net](http://jrhicks.net/Projects/rails/)  
has\_many\_and\_belongs\_to\_many.pdf

On a similar note:  
How does rails handle the plural expansion of words like ‘category’.  
i.e., if you have a migration called categories is it going to  
correctly use the singular for the model? That kind of naming scheme  
doesn’t seem to make sense in enough situations, but anyway…

Thanks in advance…

## with regards,

Lachlan Deck

---

<div class="post-metadata">

**Author:** ![Lachlan\_Deck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Lachlan\_Deck](https://www.ruby-forum.com/u/Lachlan_Deck)\
**Post date:** [April 10, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378/2 "2006-04-10T17:19:42Z")

</div>

On Apr 10, 2006, at 6:16 AM, Lachlan Deck wrote:

> have
> 
> ITEMS \<\<–\> ITEMS\_CARTS \<–\>\> CARTS
> 
> where the join table, items\_carts, has the dual primary keys  
> {items\_id, carts\_id}.

The Rails default naming for HABTM tables is alphabetical order, so  
that table  
should be:

```
CARTS <<--> CARTS_ITEMS <-->> ITEMS

```

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Lachlan\_Deck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Lachlan\_Deck](https://www.ruby-forum.com/u/Lachlan_Deck)\
**Post date:** [April 10, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378/3 "2006-04-10T19:41:38Z")

</div>

Hi there,

On 11/04/2006, at 1:16 AM, Tom M. wrote:

> > 
> 
> should be:
> 
> CARTS \<\<–\> CARTS\_ITEMS \<–\>\> ITEMS

No problems (just a typo) but my real question is how do you define a  
migration for CARTS\_ITEMS so that it has the following attributes?

- carts\_id (primary key)
- items\_id (primary key)
- quantity

## with regards,

Lachlan Deck

---

<div class="post-metadata">

**Author:** ![Lachlan\_Deck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Lachlan\_Deck](https://www.ruby-forum.com/u/Lachlan_Deck)\
**Post date:** [April 10, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378/4 "2006-04-10T20:15:24Z")

</div>

00X\_create\_carts\_items.rb

class CreateCartsItems \< ActiveRecord::Migration  
def self.up  
create\_table :carts\_items, :id =\> false do |t|  
t.column :cart\_id, :integer  
t.column :item\_id\_id, :integer  
t.column :quantity, :integer  
end

```
# there is no way to specify multiple primary keys
execute "ALTER TABLE carts_items ADD PRIMARY KEY (cart_id, item_id)"

# alternative
# add_index :carts_items, [:cart_id, item_id], :unique => true

```

end

def self.down  
# drop\_index :cart\_items, :cart\_id # use if using alternative to  
primary  
keys  
drop\_table :carts\_items  
end  
end

---

<div class="post-metadata">

**Author:** ![Lachlan\_Deck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@Lachlan\_Deck](https://www.ruby-forum.com/u/Lachlan_Deck)\
**Post date:** [April 10, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/many-to-manys-join-table-attributes-migrations/57378/5 "2006-04-10T20:49:12Z")

</div>

Hi there,

On 11/04/2006, at 4:12 AM, Chris H. wrote:

> ```
> # there is no way to specify multiple primary keys
> 
> ```
> 
> keys  
> drop\_table :carts\_items  
> end  
> end

Thanks Chris.

> > No problems (just a typo) but my real question is how do you define a  
> > migration for CARTS\_ITEMS so that it has the following attributes?
> > 
> > - carts\_id (primary key)
> > - items\_id (primary key)
> > - quantity
> > 
> > ## with regards,
> > 
> > Lachlan Deck

## with regards,

Lachlan Deck
