Fake it till you make it!


I’d like to share another handy little rake task that helps you fill up your Rails application with realistic ‘fake’ data. Here it is

Using the very excellent Faker gem by Benjamin Curtis this task can be configured to populate your models with random amounts of fake information. This can a be a real time-saver for load testing, preparing demos/screencasts, or just filling up your pages with realistic data so you can get to work on your views.

The classic 'comb-over', a traditional fake.

The classic 'comb-over', a traditional fake.

The task includes the following features;

  • Assign relationships between models with a random object picker
  • A random tag picker, for populating tag lists
  • Fake out your app at different different sizes; tiny, small, medium and large
  • Clean task, to clear stuff out before you start your faking
  • Disables mail sending before faking out, since before/after and other code hooks in your app might trigger sending mails
  • Helper to fake out a randomly picked time object within the last year
  • Prompts to ask before deleting any data on cleaning task.
  • Summary report after faking out, explaining exactly how many objects were created

Simply configure it (see code comments for help on this), drop it into your Rails lib/tasks folder and run like so;

sudo gem install faker # if you haven't got the gem already OR 
sudo gem install ffaker # use the faster faker gem from Emmanuel Oga 
rake fakeout:small # or tiny / medium / large

Cleaning all ...
Faking it ... (small)
  * Users: 53
  * Questions: 53
  * Answers: 38
  * Tags: 38
  * Taggings: 160
Done, I Faked it!

Along with this subdomain script from last month, I have pushed this code to github as a gist, so you can track it and grab updates to it whenever. The example there shows the task configured for a small Question & Answer app I’ve been working on.

And yes, I have started drawing a bit again, maybe it’ll make these posts more interesting to look at until I properly build this blog out.

Update – I have amended the task definitions to take a single argument; no_prompt. This turns off the confirm prompt and is useful for non-interactive running, e.g. with heroku rake commands for instance.

Update – This rake task is also fully compatible with the faster ffaker gem by Emmanuel Oga

February 07, 2010 15:31 by

2 comments so far

  • photo of Denis Savitsky Denis Savitsky May 08, 2012

    How can i overcome mass_assignment protection gracefully? Now i have to add and remove :user_id each time i have to repopulate my db. :( Is there any way to generate random images for gallery?

  • photo of Matt Matt May 10, 2012

    Since the faker always calls, model.constantize.create!(attributes) this means in its current incarnation, you can’t pass attributes that are protected from mass assignment.

    To generate random images for a gallery, you could put a bunch of images in a test or data folder and name them; image_1.jpg image_2.jpg etc. – then in this script assign the image to the model using the fixture_file_upload method.

    FYI, this is quite an old blog post and gist (2+ years!)

    My new approach to faking things out <a href="https://gist.github.com/2654461">goes a little like this</a>

Leave a comment