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

Loading fixtures

Fixtures contain data which can be loaded into your database using migrations. For example, to load data into a table named customers...

  • Create a directory, db/migrate/data
  • Create a file, customers.yml, inside db/migrate/data
  • Generate a new migration file: ruby script/generate migration load_customers_data
  • Edit it to load data from the customers.yml file into your customers table

customers.yml

    melissa:
      name: Melissa
      age: 18
    david: 
      name: David
      age: 23   

migration.rb

    require 'active_record/fixtures'
    class LoadCustomerData
     def self.up
      down
      directory = File.join(File.dirname(__FILE__), "data")
      Fixtures.create_fixtures(directory, "customers")
     end
     def self.down
      Customer.delete_all
     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