API reference for Migrations

The following table details the methods that are available in the context of create_table
and change_table methods within a migration class.

change(column—name, type, options = {})
Changes the column’s definition according to the new options. The options hash optionally contains a hash with arguments that correspond to the options used when adding columns.
t.change(:name, :string, :limit => 80)
t.change(:description, :text)


change—default(column—name, default)
Sets a new default value for a column.
t.change_default(:qualification, ‘new’)
t.change_default(:authorized, 1)
column(column—name, type, options = {})
Adds a new column to the named table.
t.column(:name, :string)
Note that you can also use the short-hand version by calling it by type. This adds acolumn (or columns) of the specified type (string, text, integer, float, decimal, datetime, timestamp, time, date, binary, boolean).
t.string(:goat, :sheep)
t.integer(:age, :quantity)
index(column—name, options = {})
Adds a new index to the table. The column_name parameter can be one symbol or an array of symbols referring to columns to be indexed. The name parameter lets you override the default name that would otherwise be generated.
# a simple index
# a unique index
t.index([:branch_id, :party_id], :unique => true)
# a named index
t.index([:branch_id, :party_id], :unique => true, :name =>’by_branch_party’)
belongs—to(*args) and references(*args)
These two methods are aliases to each other. They add a foreign key column to another model, using Active Record naming conventions. Optionally adds a _type column if the :polymorphic option is set to true.
create_table :accounts do
create_table :comments do
t.references(:commentable, :polymorphic => true)
Removes the column(s) specified from the table definition.
t.remove(:qualification, :experience)


remove—index(options = {})
Removes the given index from the table.
# remove the accounts_branch_id_index from the accounts table
t.remove_index :column => :branch_id
# remove the accounts_branch_id_party_id_index from the accounts table
t.remove_index :column => [:branch_id, :party_id]
# remove the index named by_branch_party in the accounts table
t.remove_index :name => :by_branch_party


remove—references(*args) and remove—belongs—to
Removes a reference. Optionally removes a type column.
t.remove_references(:commentable, :polymorphic => true)

Here’s a method that you will never use, unless you forgot to add timestamps in the
create_table block and do it in a later migration. It removes the timestamp columns.
(created_at and updated_at) from the table



rename(column—name, new—column—name)
Renames a column. The old name comes first, a fact that I usually can’t remember.
t.rename(:description, :name)
Adds Active Record-maintained timestamp (created_at and updated_at) columns to the table.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s