Ruby on Rails ActiveRecord Migrations Create a join table

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

To create a join table between students and courses, run the command:

$ rails g migration CreateJoinTableStudentCourse student course

This will generate the following migration:

class CreateJoinTableStudentCourse < ActiveRecord::Migration[5.0]
  def change
    create_join_table :students, :courses do |t|
      # t.index [:student_id, :course_id]
      # t.index [:course_id, :student_id]
    end
  end
end


Got any Ruby on Rails Question?