Prepopulating Database Tables - Migrations?


I’m starting to get into using migrations, but I’m trying to figure out
the best practice for prepopulating database tables. I’m talking about
support tables that need to have data before the application can run.

Before I just had a sql script with all the drop and create statements,
and some insert statements at the bottom.

I know there are a million ways to do this, but I was just checking to
see if I somehow missed the magical Rails way.

Any thoughts would be most helpful.


I had this same issue, here’s what I did:

I put the data that I needed to populate into a .yml file, structured
with the table name as the key, and the value as an array of hashes,
like so:


  • { email: removed_email_address@domain.invalid, password: bar }
  • { email: removed_email_address@domain.invalid, password: rockin }


  • { message: “Good to see you again!” }

Where the keys in the hashes correspond to AR attributes.

Then, it was a simple matter to use the YAML classes to read this file,
iterate over all the keys, then, for each key, iterate over the array
entries, using the hash keys/value to create new records.

Finally, I created a custom Rake task called “seed_database” that
actually drives the whole process. This also made it to integrate this
process into my Capistrano deployment.