Rails – real data – database loading

Quick tip…

When working on a rails project you need to have REAL DATA to work with in your development environment. The easiest way is to grab data from a production database. Ah, but won\’t that require SQL and effort?

The answer is no. Luckily, 99% of the work involving this is done for you. You will not have to code a single bit of SQL to accomplish this.

Here is what we do here:

  1. Find the extract_fixtures rake task somehwere out on the internet. Check it into your project. Essentially, this gives you a rake task that dumps the environments database into .yml files in your test/fixtures directory. I modified the file so that it creates files with a \’.bak\’ extension in order to avoid destroying an existing fixture.

So, on my production machine, I do the following.


rake extract_fixtures RAILS_ENV=production

Then I copy the resulting files to my development environment. I then change the data to protect the innocent and avoid leaking any sensitive information. Once that is done, I rename the files over their blank/empty/unused fixtures of the same name (for example mv fruit.yml.bak fruit.yml) and check them in.

Once all the fixtures are sufficiently populated, I then do the following on my development workspace:


rake db:fixtures:load

I have just given myself (and every developer on the team) great test data that is also useful for developing new features. It is just too hard to code new features without real data. For example, that bar chart you are working on will not impress anyone when all the bars are at 0. 🙂

So, again, a few simple,easy steps and significant improvement on the development cycle for all parties.

This entry was posted in General, Ruby and Rails. Bookmark the permalink.