A normal table¶
class CreateRobots < ActiveRecord::Migration
def self.up
create_table :robots do |t|
t.string :name
t.integer :energy_level
t.text :biography
t.timestamps
end
end
def self.down
drop_table :robots
end
end
A join table¶
When creating a has_and_belongs_to_many
relationship, the join table that is used should not have an id column. Instead, there are id columns for each of the foreign keys in the relationship. In order to prevent the automatic id
column from getting created, pass :id => false
to create_table
.
class CreateMemberships < ActiveRecord::Migration
def self.up
create_table :memberships, :id => false do |t|
t.column :group_id, :integer
t.column :user_id, :integer
end
end
def self.down
drop_table :memberships
end
end