You are here: Home Solidarrow Ruby on Rails Solidarrow Cheatsheets

* Rails Migrations Cheatsheet

Up and down methods, rake tasks, column mapping, loading fixtures, example migration file, all packed onto one A4 page for an easy at-a-glance reference. Updated for Rails 2.1

Table methods

change_table

Provides a block that enables you to alter columns on an existing table using various shortcut methods...

    change_table :table_name,  {options} do |t|
      t.change :column_name, :new_column_type
      t.remove :column_name
    end

create_table

Creates a table on the database. Creates a table called :table_name and makes the table object available to a block that can then add columns to it by specifying column_types or utilising shortcut methods such as using belongs_to to specify foreign keys...

    create_table :table_name, {table_options} do |t|
      t.string :name, {column_options}
    end

{table_options}

:force

true or false

if true, forces drop of an existing table of the same name before creation the new one

:temporary

true or false

if true, creates a temporary table, one that goes away when the application disconnects from the database

:id

true or false

if false, defines a table with no primary key, for example when you need to define a join table

:primary_key

:symbol

overrides the default name of :id for the primary column. Use this to specify the name of the column in the database that Rails will use to store the primary key

:options

"string"

pass raw options to your underlying database, e.g. auto_increment = 10000. Note that passing options will cause you to lose the default ENGINE=InnoDB statement

drop_table

Destroys the specified table.

    drop_table :table_name

rename_table

Renames the specified table.

    rename_table :old_table_name, :new_table_name



 

 

> RELATED ARTICLE

* New in Rails 2.0: Sexy Migrations

A look at the new features and syntax available in migrations on Rails 2.0 > More

 

> RELATED ARTICLE

* Auto migrations

A brilliant plugin that makes changing your database schema even easier and faster. > More