3 Ways To Build Fake Demo Data For Your Rails App
You've finished the first sprint of your new Rails application and you're ready to show it to the client - or are you? Most user interfaces look better when they're actually filled with data. You could sit down and start entering records by hand, wracking your brain to think of new names and addresses. Or you could take the easier way out by turning to a random data generation library.
We previously looked at the Faker library from Ben Curtis. Now at version 0.3.1, Faker is a port of Data::Faker from perl. Faker includes modules to generate random names, addresses, phone numbers, and more. Faker's data is hard-wired as lookup arrays in its source code, though, and it's installed as a gem, making it somewhat difficult to extend. Two other data generators are easier to work with in this regard.
The first of these is the random-data gem, currently at version 1.5.0. The brainchild of Mike Subelsky, random-data has a somewhat wider range than faker, covering names, dates, text, addresses, phone numbers, and more. As with Faker, the basic building blocks of the data are hardwired in the source code, but random-data also implements method_missing
in a useful way: if you have a file named "spam.dat" in your load path, then calling Random.spam
will return random lines from the file.
Rails developers will also want to check out Nate Sutton's forgery plugin. Forgery uses a Rails generator to move its data into your application, and it also separates data dictionaries from the actual generation code, making it extremely simple to change things like the list of names or cities that it includes. Forgery also has more data to choose from than the other two libraries, leading to an increased appearance of randomness in the end product.
Is this the end of the test data generation story? Probably not. At least random-data and forgery are still under active development, and I can think of some useful ways to extend them. For example, it would be great to see some sort of correlated generator that would ensure that a state, zip code, and area code all matched up roughly with one another. But even in their current state, any of these libraries will do better to fill out your test user interface than laboriously putting in everything by hand.
Tip: There's a more basic guide on generating fake names, addresses and more in Ruby over at Ruby And How!
November 14th, 2008 at 4:54 pm
I've used faker before and it works really good. A huge benefit to all these is you can test your application with large quantities of data easily. Something that is fast with 10 records might crash with 1000 records.
November 14th, 2008 at 5:14 pm
im using populator http://github.com/ryanb/populator/tree/master
which is build to work with faker, had no problems so far
November 15th, 2008 at 1:46 pm
There is good video on populating a rails db with data at Railscasts: http://railscasts.com/episodes/126-populating-a-database
November 15th, 2008 at 4:49 pm
Thanks for writing about random-data. I hear you about the hardwired fake data. The idea of it is to get you up and running fast, with method_missing there if you want something different. If enough people want to be able to specify their own fake information we could definitely add that. Most of the last few releases were driven by people submitting patches, so all are welcome!
November 15th, 2008 at 6:31 pm
Very useful, nice.
November 16th, 2008 at 10:28 am
I've been using faker, its very nice. Also, I've been unpacking gems a lot lately, and I find that the way that faker works is pretty easy to customize to my needs.
February 18th, 2009 at 1:38 pm
There is also the WONDERFUL GameFake gem I wrote as my first Ruby gem. :-)
https://github.com/cbmeeks/gamefake/tree
It's Faker but uses video game names instead. Currently, I have NES and SMS games. I might expand more in the future.
It was mostly created as a learning experience and for a site I'm working on.
March 25th, 2009 at 4:11 pm
Hi there!, I am in the final spring in this rails app but I need to populate a few tables with huge quantities of data, I need 1'000,000 one million registers!. what fake data program do you recommend?