Let’s have two models having has and belongs to many associations.
class Contact < ActiveRecord::Base
has_and_belongs_to_many :phones
end
and
class Phone < ActiveRecord::Base
has_and_belongs_to_many :contacts
end
Now migrations will be written as
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
class CreatePhones < ActiveRecord::Migration
def change
create_table :phones do |t|
t.string :number
t.timestamps
end
end
end
class CreateJoinTableContactPhone < ActiveRecord::Migration
def change
create_join_table :contacts, :phones, column_options: {null: true} do |t|
end
end
end