Lachlan Deck
2006-Apr-10 13:16 UTC
[Rails] Many To Many''s Join table attributes + migrations
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
Tom Mornini
2006-Apr-10 15:16 UTC
[Rails] Many To Many''s Join table attributes + migrations
On Apr 10, 2006, at 6:16 AM, Lachlan Deck wrote:> 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}.The Rails default naming for HABTM tables is alphabetical order, so that table should be: CARTS <<--> CARTS_ITEMS <-->> ITEMS -- -- Tom Mornini
Lachlan Deck
2006-Apr-10 17:40 UTC
[Rails] Many To Many''s Join table attributes + migrations
Hi there, On 11/04/2006, at 1:16 AM, Tom Mornini wrote:> On Apr 10, 2006, at 6:16 AM, Lachlan Deck wrote: > >> 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}. > > The Rails default naming for HABTM tables is alphabetical order, so > that table > should be: > > CARTS <<--> CARTS_ITEMS <-->> ITEMSNo 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
Chris Hall
2006-Apr-10 18:12 UTC
[Rails] Many To Many''s Join table attributes + migrations
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 On 4/10/06, Lachlan Deck <lachlan.deck@gmail.com> wrote:> > Hi there, > > On 11/04/2006, at 1:16 AM, Tom Mornini wrote: > > > On Apr 10, 2006, at 6:16 AM, Lachlan Deck wrote: > > > >> 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}. > > > > The Rails default naming for HABTM tables is alphabetical order, so > > that table > > 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060410/19023c24/attachment.html
Lachlan Deck
2006-Apr-10 18:47 UTC
[Rails] Many To Many''s Join table attributes + migrations
Hi there, On 11/04/2006, at 4:12 AM, Chris Hall wrote:> 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 > endThanks 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 Deckwith regards, -- Lachlan Deck