ActiveRecord Migrations Cheat Sheet

I’ve been googling and weeding through migration syntax far too often to not make a cheat sheet, so I’ve listed out some of my most commonly used migrations. Without further ado…

ActiveRecord Migrations Cheat Sheet!

Create a table

rails g migration CreateKittens name:string cuteness_level:integer adopted:boolean

Create a join table

rails g migration CreateJoinTableKittenUser kitten user

Alternatively, use resource to create the migration along with the model and controller

rails g resource kitten name:string cuteness_level:integer adopted:boolean
$rails g resource owner kitten:belongs_to user:belongs_to

Add column whiskers to table kittens:

rails g migration AddWhiskersToKittens whiskers:string

Add columns age and weight to table kittens:

rails g migration AddHealthToKittens age:integer weight:integer

Remove column eye_color from table kittens

rails g migration RemoveEyeColorFromKittens eye_color:string

Remove just created migration

rails d migration AddWhiskersToKittens whiskers:string

As always, don’t forget to

rake db:migrate
Where are we migrating??