Rails has_and_belongs_to_many

Let we consider two models like User and Team rails g migration User email:string name:string rails g migration Team name:string then inside User class ofUser model, type has_and_belongs_to_many :teams then inside Team class of Team model, type has_and_belongs_to_many :users again terminal create new migration like rails g migration create_teams_users_table Important note is you are combining two model name like teams_users. here two models has to set according to alphabetical order, t__u. inside teams_users migration type def CreateTeamsUsersTable < ActiveRecord::Migration create_table :teams_users, :id => false do |t| t.references :user t.references :team end add_index :teams_users, [:team_id, :user_id] end finally rake db:migrate have an enjoy. If you have further clearance, please comment this page.