Topic: Many-to-Many Migration with Indexes questions
The answer to this question is probably really simple, but I'm fairly new to rails and can't find any detailed information about this subject--not even in the Agile Web Development with Rails Book (2nd Edition).
The questions deal with many-to-many relationships in rails. I've got some database background so the concept of many-to-many isn't foreign to me.
First off, should I create all three tables (categories, products, and categories_products) in a single migration? If so, should I create the migration as "ruby script/generate migration categories_products"? Assuming I'm correct so far, what do I need to do for the category and product models (since Rails didn't auto-create these because I used ruby/script generate migration for 3 tables rather than ruby script/generate model for each part of the many-to-many table)?
On a related noted, I would like to create two indexes for the join table in the same migration as the many-to-many tables:
add_index :categories_products, [:product_id, :category_id]
add_index :categories_products, :category_id
What is needed for the def self.down part of the migration and in what order? I was thinking:
remove_index :categories_products, [:product_id, :category_id]
remove_index :categories_products, :category_id
but I don't think the remove Index parts are right.
One more question: The create_table :categories_features, id => false do |t| doesn't seem to work in my migration file. When I run the migration and examine the tables using Cocoa MySQL, the join table has an id field even though I used id => false. Any ideas?
Almost forgot, I also am trying to create database constraints in this migration file even though Rails doesn't technically need them.
Any and all help will be greatly appreciated. If anyone knows of a good up-to-date tutorial (for Rails 1.2+), please share. Thanks in advance for your help!