Using The ‘Faker’ Ruby Gem

Patrick Brennan
4 min readJan 5, 2022

In the course of building out my Ruby on Rails and JavaScript projects for the Flatiron School’s Software Engineering program, I took extra care in crafting every single piece of data that went into my database seed file myself. Part of this was due to my own excessive fastidiousness, but the primary reason for this was because of the nature of these projects. My JavaScript project — JavaScript Jeopardy — necessitated real-life Jeopardy questions, answers and point values to mimic the real-world mechanics of Jeopardy. My Rails project was called Bandbuilder. Bandbuilder centered around Music Directors creating Gigs and booking Musicians for those gigs — so to properly demonstrate the scope of my app, I created a database that used known musicians and music directors as well as real-life venues for each gig. You could argue that it wasn’t totally necessary to do this, but at the time I knew no other way to seed my database.

When presenting Bandbuilder to my section lead at Flatiron, though, he informed me of the existence of Faker, a magical ruby gem that creates dummy data for you with little more than a set of simple methods and a .times loop. While I couldn’t use it for JavaScript Jeopardy, I could use it for my final project: Cinemaddict. Cinemaddict is a web app centered around movie reviews, in which users can rate and review their favorite movies. While the front-end of my project was primarily built using JavaScript’s React and Redux frameworks, the back-end is a good old fashioned Rails API.

This Rails API is centered around three models: User, Review and Movie, and I needed seed data for each. I wanted to use real movie data. After some digging, I was able to pull some JSON data on the Top 250 movies on IMDB from IMDB’s API, and, after some reformatting, my movie seeds were squared away. As for my users and reviews, I didn’t want to take precious time creating users and reviews one at a time, especially when that time could be better spent shaping the functionality of my app, so I added the faker gem to my back-end via the ‘bundle add faker’ terminal command and I was ready to fake some data!

First I needed to create some users. Using Faker’s default generators and their respective, I was able to create randomized but functional data. To generate unique usernames, for instance, I simply invoked Faker’s Twitter screen name generator with the following line: Faker::Twitter.screen_name. Similarly, I was able to generate passwords with Faker::Alphanumeric, which creates a randomized alphanumeric string at the length of your choosing, as well as user bios with Lorem Ipsum dummy text using Faker::Lorem.paragraph. I was even able to generate an avatar for each user with Faker::Avatar.image, which produces a link to a random image of a cartoon robot, which could be used as the src in an img HTML tag. Wrapping it all in a 20.times loop, I was able to create 20 unique users in an instant.

Now that my users were squared away, it was onto crafting some fake movie reviews. This required a little more effort than simply writing Faker::Lorem.sentence, but after a slight touch of coding wizardry to “titleize” my review titles, I was able to craft 250 unique reviews using the Faker::Lorem generator, and associate each review with my newly minted fake users and my vast selection of real movies.

And there you have it! If you’re interested in the many ways you can create fake data for your Ruby/Rails projects, check out the Faker gem’s GitHub repo at https://github.com/faker-ruby/faker.

--

--

Patrick Brennan

Charting my journey from coding noob to professional programmer.