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

example_migration.rb

    class CreateCustomers < ActiveRecord::Migration  

      def self.up
        create_table :customers, :primary_key => :customer_id, :options => "auto_increment = 10000" do |t|
          t.integer :customer_id
          t.string :name,         :limit   => 30, :null => false
          t.integer :age
          t.boolean :premium,     :default => 0
          t.binary :photo,        :limit   => 2.megabytes
          t.timestamps
          t.text :notes,          :default => "No notes recorded"
        end

        add_column :customers, :surname,  :string,   :limit => 50    

        add_column :orders,    :price,    :decimal,  :precision => 8, :scale => 2

        Customer.create :name => "David", :surname => "Smith", :age => "32", :premium => "1", :notes => "One of our top customers!"
      end

      def self.down 
        drop_table :customers
      end
    end



 

 

> 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