Generate migration - create join table Generate migration - create join table ruby-on-rails ruby-on-rails

Generate migration - create join table


To autopopulate the create_join_table command in the command line, it should look like this:

rails g migration CreateJoinTableProductsSuppliers products suppliers

For a Product model and a Supplier model. Rails will create a table titled "products_suppliers". Note the pluralization.

(Side note that generation command can be shortened to just g)


Run this command to generate the empty migration file (it is not automatically populated, you need to populate it yourself):

rails generate migration assignments_security_users

Open up the generated migration file and add this code:

class AssignmentsSecurityUsers < ActiveRecord::Migration  def change    create_table :assignments_security_users, :id => false do |t|      t.integer :assignment_id      t.integer :security_user_id    end  endend

Then run rake db:migrate from your terminal. I created a quiz on many_to_many relationships with a simple example that might help you.


I usually like to have the "model" file as well when I create the join table. Therefore I do.

rails g model AssignmentSecurityUser assignments_security:references user:references