Generate migration - create join table

Ruby on-RailsRuby on-Rails-3Ruby on-Rails-4MigrationJointable

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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)

Solution 2 - Ruby on-Rails

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
  end
end

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.

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

There's embeded generation for join table in rails

rails g migration AddCityWorkerJoinTable cities:uniq workers

which generates following migration

create_join_table :cities, :workers do |t|
  t.index [:city_id, :worker_id], unique: true
end

❗️ NOTE:

  • models names should go in alphabet order
  • put :uniq only on first param

Solution 5 - Ruby on-Rails

I believe this would be an updated answer for rails 5

create_table :join_table_name do |t|
  t.references :table_name, foreign_key: true
  t.references :other_table_name, foreign_key: true
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongotqnView Question on Stackoverflow
Solution 1 - Ruby on-RailsandrewcockerhamView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPowersView Answer on Stackoverflow
Solution 3 - Ruby on-RailsPrakash RamanView Answer on Stackoverflow
Solution 4 - Ruby on-RailsitsnikolayView Answer on Stackoverflow
Solution 5 - Ruby on-RailsCody ElhardView Answer on Stackoverflow