Using the Faker-Ruby gem by Faker

Or fake it until you make it.

Alex Amersi-Belton
3 min readJan 26, 2022


Project four of my Software Engineering bootcamp rolls around and the requirement to have a Rails API backend was a fundamental part of the brief.

A backend, with data in it, how would I get that? Sure, there are some great free API that you can utilise for learning purposes, however I was looking for something that I had full control over as I pushed for my MVP.

A lot of my course learning material kept using database seeding with some seemingly random code called Faker::RandomData (or words to that effect). This naturally piqued my interest, fake the data, test the hypothesis, and once it’s working, I can put a full build with a clean database. The question is what is this Faker and how does it work? The following is my experience while working it out.

What is Faker-Ruby?

A quick search and I had located Faker-Ruby, the ruby focused package which generates fake data for your API/Database needs on Ruby/Rails APIs.

Sounds promising. What does that mean? Well as my preamble may have indicated, when you need a set of data in your database for testing this can help you create the seed data for testing. While it doesn’t guarantee unique values, you can specify when you do require them and Faker will do it’s best to make it happen.

How does it work?

First install the gem: gem install faker

Make sure it’s in your gem file and required within your app and it should go.

Lets fake some data! As part of my project I needed a small sample of users for some app users. Here’s what I added to my seed file:

Faking five users for my app

I asked this snippet to run 5 times, and interact with the username column of my table. It then activates the Faker package and uses the Ancient generator, specifically the heroes, I then asked for it to be in lower case, and finally I used Ruby’s global substitution gsub method along with the regex to substitute white space for an underscore.

The end result, Achilles, Dante, Hermione, Ganymede and Ajax became the first five users of my app, in lower case of course!

The astute amongst you may note I have used a user = User.create method, this is because I later needed some of my users to become foreign ID keys in other tables within my API structure.

Some oversights that I made so you don’t have to

My API had three separate methods being seeded, including a many-to-many relationship. Initially I tried to populate the many-to-many table before the two foreign keys were fully faked, this meant the request was ignored, some hasty re-arrangement of the order and it worked.

I then refactored the seed code to use the create User seed to also be the lead foreign key in the many-to-many relationship table. This enabled me to use a rand(5..10).times do so my users had between 5 and 10 of the different McGuffins from the static table. Handy.

Bonus: the quickest way to reset and reseed a db is to use the rails db:reset this drops and recreates the database from db/schema.rb for the current environment and loads the seeds. Which if you were happy with your models and table setups allows you to tweak the data you seed.

Thanks for joining me for this journey into Faker-Ruby, happy faking.



Alex Amersi-Belton

Former tech banker, now aspiring software engineer. Feel free to get in touch.