views:

853

answers:

4

Best way to load seed data? I have an Author table that is tightly coupled with a Users table. I also have migrations to alter both of these tables. I want to add a default admin user but I want to make sure that both tables are created and all migrations have run for these tables before my CreateDefaultAdmin (or whatever) migration runs. Is there a best practices for doing this? Sorry if this was already asked.

A: 

cant you just create seperate migrations and run them seperatly

ADAM
+1  A: 

The latest (edge?) version of Rails includes a db/seeds.rb file into which you can place code to create records in your database. Until then migrations are the way to go, but they can be messy when the schema keeps changing (which is the entire point of migration files).

askegg
+5  A: 

Your options:

  1. Use migrations as outlined here: http://railspikes.com/2008/2/1/loading-seed-data
  2. Use a 3rd party addon like: seed-fu or db-populate
  3. Use fixtures, which is outlined in the rails spike article.
  4. Wait for rails 3 and use the new seeds.rb: http://github.com/rails/rails/commit/4932f7b38f72104819022abca0c952ba6f9888cb

Personally I use a modified yaml-db. I like to build up all my seed data into my dev environment, and keep it backed up in my repository, when I go live I can load it with a rake task.

Sam Saffron
A: 

Try Factory Girl as a fixtures replacement for tightly coupled models:

http://www.thoughtbot.com/projects/factory_girl

cakeforcerberus