views:

72

answers:

1

Hi everyone,

I have a large database I need to populate with data before initially running the application. Is it best to do this in an external rake task or inside migrations?

Sincerely, Tyler

+1  A: 

The file db/seeds.rb is designed for this purpose. It should have been generated automatically when you created the Rails project. It has a comment at the top that explains how to use it:

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#   
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Major.create(:name => 'Daley', :city => cities.first)

After you've populated it, use rake db:seed to populate the database with the seed data.

rspeicher
I think this file only exists from Rails 3 onward. But if you want to use it earlier, there shouldn't be a problem with just making the file and adding the rake task yourself.
Matchu
Not true. `[~/Code/rails] rails --versionRails 2.3.5[~/Code/rails] rails seeds create ... create db/seeds.rb`
rspeicher