Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migrations: Load default data

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.

like image 979
Rob Avatar asked Aug 08 '09 04:08

Rob


People also ask

What is are the default column columns that rails will generate while migrating?

You can append as many column name/type pairs as you want. By default, the generated migration will include t. timestamps (which creates the updated_at and created_at columns that are automatically populated by Active Record).

How rails db Migrate works?

When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration.

What is schema RB in rails?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone. With a present schema.


1 Answers

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.

like image 162
Sam Saffron Avatar answered Oct 07 '22 22:10

Sam Saffron