New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

3 Ways To Build Fake Demo Data For Your Rails App

In Compilation Posts, Plugins, Tools

numbers-1.pngYou'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!

Post to Twitter Tweet This Post

Vaguely Related Posts (Usually)

8 Comment Responses to “3 Ways To Build Fake Demo Data For Your Rails App”

  1. #1
    Eric Davis Says:

    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.

  2. #2
    grosser Says:

    im using populator http://github.com/ryanb/populator/tree/master
    which is build to work with faker, had no problems so far

  3. #3
    Luis Lebron Says:

    There is good video on populating a rails db with data at Railscasts: http://railscasts.com/episodes/126-populating-a-database

  4. #4
    Mike Subelsky Says:

    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!

  5. #5
    carlos@webbynode Says:

    Very useful, nice.

  6. #6
    Scott Ballantyne Says:

    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.

  7. #7
    cbmeeks Says:

    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.

  8. #8
    Alan Maciel Says:

    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?

Leave a Reply