# Prepopulating Database Tables - Migrations?

**URL:** <https://www.ruby-forum.com/t/prepopulating-database-tables-migrations/62928>\
**Category:** Rails\
**Created:** [June 9, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/prepopulating-database-tables-migrations/62928 "2006-06-09T17:00:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [June 9, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/prepopulating-database-tables-migrations/62928/1 "2006-06-09T17:00:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thabenksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thabenksta](https://www.ruby-forum.com/u/thabenksta)\
**Post date:** [June 9, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/prepopulating-database-tables-migrations/62928/2 "2006-06-09T17:31:03Z")

</div>

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:

users:

- { email: removed\_email\_address@domain.invalid, password: bar }
- { email: removed\_email\_address@domain.invalid, password: rockin }

messages:

- { 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.

Best,

Danny
