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.
+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
2009-08-08 05:03:28
+5
A:
Your options:
- Use migrations as outlined here: http://railspikes.com/2008/2/1/loading-seed-data
- Use a 3rd party addon like: seed-fu or db-populate
- Use fixtures, which is outlined in the rails spike article.
- 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
2009-08-08 05:25:19
A:
Try Factory Girl as a fixtures replacement for tightly coupled models:
cakeforcerberus
2009-08-12 15:26:25