Faker It Til You Make-r It; An Exploration in Efficiency in Rails Development

Nick Galante
4 min readJun 18, 2020

--

Ladies, gents, ones, and zeroes- I am here to speed up your web development foundational time. Don’t think of me as Nick anymore, think of me as WD40 for the Rails that your app is running on. Allow me to introduce to you, the gem Faker.

https://user-images.githubusercontent.com/36028424/40263395-4318481e-5b44-11e8-92e5-3dcc1ce169b3.png

Okay, so let’s dive in. I know for a fact that I, and you don’t have to admit it but I know you have too, have spent way too much time on creating seed data for your applications. Do you want to know how I know that? Because I’m sure you’ve spent more than four seconds on it. But see that’s counterproductive. We’re escaping the utility of seeds when we do it manually as such. I look back to a number of the projects I have stashed away from when I was just a little infant developer (maybe three months ago), and my seeds.rb files are a source of repeated PTSD. I would labor forever racking my brain to muster one creative thought or example over the next, but I’m not writer for Rick and Morty, I’m a developer, and my efforts are better spent on function, than spent on creating examples to test that function against. So to speed up that example setting, the team over at Faker decided to help us out and do the creative thinking for us- sounds good right?

Let’s start with install. It’s a Ruby gem, so like all other Ruby gems, you have to include it in your Gemfile to later utilize it in your code. After you’ve got that set, just run a quick ‘bundle install’ to get cooking.

Awesome, be sure to include require ‘faker’ at the top of the file you are trying to use it on as well. Now, Faker is an extensive library, and it has a multitude of different Faker classes to aid in your seeding. Some of the more popular Faker classes that you’ll want to utilize in your application might be Faker::Name, Faker::Address, Faker::PhoneNumber, and even Faker::Date and Faker::Time. Let’s explore one in depth- Faker::Name.

Here, you’re presented with a number of Faker native methods, to call with your instance of Faker::Name, to better streamline the seeding of your data. Let’s really materialize the brilliance of such a feature. You are developing an app that intends to allow a User to leave a review of a restaurant directly to another User, as perhaps a recommendation or a fair warning to stay away, and you want to begin examining how you want certain pieces of your application to interact. So what do you need to test the function of your app? Users. Now we want our app to be able to manage thousands of users potentially, so we want to seed our database with a realistic number of examples to test the program with. Let’s say even that we have a relatively simple User model even:

So our User model only accepts a name and an age. Very simple. But we want to emulate hundreds if not thousands of distinct examples for our database. And we’re probably going to run out of variations of the name Nick G soon. So we’re going to do the right thing, and let Faker do the heavy lifting.

Now, through a little clever crafting with a .times block and Faker, in the same three lines of code it took to create three examples of User in our previous seeds file, we now made 150 distinct examples in the same exact time. And that number’s arbitrary, it could be 150,000 examples- although that would take a bit. But the point here is that now, you have a database full of fantastic, distinct, example data to test with, and you can exert your brainpower less on the names of your fictitious users and more on the actual utility of your application. Your welcome!

--

--

Nick Galante

Senior Software Engineer @ Charter Communications, Previously @ Amazon Games, Amazon Prime Gaming