Many To Many's Join table attributes + migrations


#1

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): 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


#2

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.


#3

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


#4

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


#5

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