Generate migration - create join table

gotqn picture gotqn · Jul 20, 2013 · Viewed 60.2k times · Source

I have looked through many SO and google posts for generating migration of join table for has many and belongs to many association and nothing work.

All of the solutions are generating a empty migration file.

I am using rails 3.2.13 and I have two tables: security_users and assignments. These are some of things I have try:

rails generate migration assignments_security_users

rails generate migration create_assignments_security_users

rails generate migration create_assignments_security_users_join_table

rails g migration create_join_table :products, :categories (following the official documentation)

rails generate migration security_users_assignments security_user:belongs_to assignments:belongs_to 

Can anyone tell how to create a join table migration between two tables?

Answer

andrewcockerham picture andrewcockerham · Dec 19, 2013

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)