0

Im trying to create a HABTM association between my products and tags tables. I ran the automated migration in my terminal as:

rails generate migration create_products_tags_join_table

then

rake db:migrate

After I ran those commands I assumed that my migration file would look something like what I read on the http://guides.rubyonrails.org/association_basics.html But instead my migration file looks like this

class CreateProductsTagsJoinTable < ActiveRecord::Migration
  def up
  end

  def down
  end
end

Do I need to hard code in the migration within these methods and then run the rake db:migrate command again?

bigREDcode
  • 159
  • 1
  • 3
  • 11

2 Answers2

0

There are way's you can name your migration to get the migration auto generate definition tags but I just do these types of things manually.

In your Case you need 3 tables:

  • products
  • tags
  • products_tags (alphabetical order matters here. This is your join table).

Products Table Migration

class CreateProductsTable < ActiveRecord::Migration
  def change
    create_table :products do |t|
      # whatever you want here...
    end
  end
end

Tags Table Migration

class CreateTagsTable < ActiveRecord::Migration
  def change
    create_table :tags do |t|
      # whatever you want here...
    end
  end
end

Join Table Migration

class CreateProductsTagsJoinTable < ActiveRecord::Migration
  def change
    create_table :products_tags , :id => false, force: true do |t|
      t.integer :product_id
      t.integer :tag_id
    end
  end
end
Andrew
  • 203
  • 2
  • 10
  • Thanks @Andrew I'll manually write the join in the migration file. Just curious do you know what the commands would be auto generate the definition tags into the migration file? – bigREDcode Sep 30 '14 at 21:16
  • i think these 3 commands might do the trick, although i didnt test. "rails g migration CreateTagsTable product:references", "rails g migration CreateProductsTable tag:references", and "rails g migration CreateJoinTableProductsTags product tag". Check the "Creating a Migration" section here: http://guides.rubyonrails.org/migrations.html – Andrew Oct 01 '14 at 20:18
0

Pretty sure if you want some stuff filled out, you'd need to generate a model, not a migration.

So, something like:

rails generate model productstags

That generates the following migration file:

20140930204336_create_productstags.rb

class CreateProductstags < ActiveRecord::Migration def change create_table :productstags do |t|

  t.timestamps
end

end end

You'd need to define any fields either manually here or in the model generation. But, if you generate a migration you have to define what you're doing. Like, say I wanted to add a join_id field that is an integer to my new ProductsTags table.

rails g migration add_join_id_to_productstags join_id:integer

That generates the following.

class AddJoinIdToProductstags < ActiveRecord::Migration def change add_column :productstags, :join_id, :integer end end

Pretty sure you just need to go that route.

Art
  • 781
  • 4
  • 13