Forum: Ruby on Rails Many To Many's Join table attributes + migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6127298119eaba07c3867e83799a8e6c?d=identicon&s=25 Lachlan Deck (Guest)
on 2006-04-10 15:19
(Received via mailing list)
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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-10 17:19
(Received via mailing list)
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 Mornini
6127298119eaba07c3867e83799a8e6c?d=identicon&s=25 Lachlan Deck (Guest)
on 2006-04-10 19:41
(Received via mailing list)
Hi there,

On 11/04/2006, at 1:16 AM, Tom Mornini 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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-04-10 20:15
(Received via mailing list)
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
6127298119eaba07c3867e83799a8e6c?d=identicon&s=25 Lachlan Deck (Guest)
on 2006-04-10 20:49
(Received via mailing list)
Hi there,

On 11/04/2006, at 4:12 AM, Chris Hall 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
This topic is locked and can not be replied to.