Skip to content

Isolating Seed Data

Richard Huang edited this page Aug 15, 2010 · 3 revisions

Please go to http://rails-bestpractices.com/posts/20-isolating-seed-data

Before:


class CreateRoles < ActiveRecord::Migration
  def self.up
    create_table "roles", :force => true do |t|
      t.string :name
    end

    ["admin", "author", "editor", "account"].each do |name|
      Role.create!(:name => name)
    end
  end

  def self.down
    drop_table "roles"
  end
end

After:


# db/seeds.rb (Rails 2.3.4)
["admin", "author", "editor", "account"].each do |name|
  Role.create!(:name => name)
end

rake db:seed

# lib/tasks/dev.rake (before Rails 2.3.4)
namespace :dev do

  desc "Setup seed data"
  task :setup => :environment do
    ["admin", "author", "editor", "account"].each do |name|
      Role.create!(:name => name)
    end
  end
end

rake dev:setup