You are here: Home Solidarrow Ruby on Rails Solidarrow Migrations

New in Rails 2.0: Sexy Migrations

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

 

Sexy Migrations is a plugin that provides a more concise syntax and a few new features for Rails migrations. Most of its functionality has now been added to the Rails 2.0 core. Let's take a look...

Shorter column syntax

If we look at an old migration file, we can see that there's usually a lot of repetition when it comes to declaring columns in a table...

    class CreateCustomer < ActiveRecord::Migration

      def self.up 
        create_table :customers do |t|

          t.column :permission_id
          t.column :class_id

          t.column :firstname,    :string, :null => false
          t.column :surname,      :string, :null => false 
          t.column :email,        :string, :null => false
          t.column :age,          :integer
          t.column :created_at,   :datetime
          t.column :updated_at,   :datetime
        end
      end

      def self.down 
        drop_table :customers
      end
    end

We declare three :string columns and make them all :null => false. We also declare three :integer columns. Spot the repetition? Wouldn't it be better if we could do this all on one line? Well now we can. The same migration file written for Rails 2.0 looks like this...

    class CreateCustomer < ActiveRecord::Migration

      def self.up 
        create_table :customers do |t|
          t.integer   :permission_id, :class_id, :age
          t.string    :firstname, :surname, :email, :null => false
          t.timestamps
        end
      end

      def self.down 
        drop_table :customers
      end
    end

Instead of the old t.column syntax where you specify the column type such as a symbol, such as :string or :integer, after declaring the column name, we now specify the column type directly as a method on t instantly saving time.

We can also specify all the column names that are to be assigned that column type, all on one line. Any options appended to the end of the line will be applied to all the specified columns, so the :string columns above will all have the null => false declaration.

What about t.timestamps? This is a shortcut for specifying Rails' created_at and updated_at automagic columns.

Foreign keys

In the above migration, we use two foreign keys, :class_id and permission_id. We could also use the new, more readable and intuitive syntax to declare these...

    t.references :class, :permission

Or we can use...

    t.belongs_to :class, :permission

...whichever naming convention you prefer, they both do the same thing. There is also a :polymorphic option for the references or belongs_to method. This is useful when you're using polymorphic associations, as it will not only create the foreign key, but also the type column as well.

For example...

    t.belongs_to :permission, :polymorphic => true

...will create the permission_id column and also a permission_type column. If you wish to declare a default polymorphic type, you can use...

    t.belongs_to :permission, :polymorphic => { :default => 'Administrator' }


 

* Comments

7 MONTHS AGO

Wonderful

Very useful tutorials. Hope more wonderful stuff.

5 MONTHS AGO

update migrations cheatsheet?

Do you have any plans to update the migration cheatsheet with this new information? I think it is a great resource, but it would be even better if it included the new Rails 2.0 migration conventions.

 

> RELATED ARTICLE

* Auto migrations

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

 

> RELATED ARTICLE

* 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 > More

 

> 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