Fake It While You Make It.
by Joseph Patterson
Here We Go Again
You are sitting at your computer, dreaming of your next big Ruby Rails project, when the inspiration strikes. You whiteboard out your models and start to see how the User will interact with your site. When you are satisfied with the initial setup, you generate models and migrations and establish their relationship macros. Time to test! You look in your empty seed file and start to put in some data to test associations. This is taking awhile. If only there was a way to input a large volume of fake data without you having to come up with it yourself. Enter Faker, your one-stop shop for test data needs!
What is Faker?
Faker is a library for generating fake data. When you need make sure you have enough data to test relationships and seed your site’s database, it is one of the best options. Quick to install and easy to use, Faker can be a real time- saver during project mode.
Installation
Faker is incredibly easy to install. First, open your gemfile and type in gem ‘faker’.
Go to the command line in your terminal for your project and type in bundle install. By default, this command will install all gems in your gemfile. You should see that at least 1 new gem was installed (faker). Thats it!! You now have access to Faker’s library of data, https://github.com/faker-ruby/faker . Now What?
Using Faker
With Faker installed its time to seed your database. This is not only fast, but fun. In the example below I am creating 10 users using Faker data.
My User model has username, name, email, and password attributes. I can browse Faker’s library of data to see if a category might hold some pertinent info for my fields.
Ahhh, Faker::Internet looks promising…
Check it out! Not only do we have options for each attribute we need but there are several variations for almost every one! We can also tweak certain categories to give us even more control of our data. Once you have found what works best for your purposes, simply copy and paste the Faker link into your User’s attribute field.
Here i have chosen to use Faker::Internet.username to automatically generate random usernames (also adding a .split(‘.’) to ensure I get the name in the format I want). Now, every time I seed the database, there will be sufficient and pertinent data for my purposes. I filled out the rest of attributes with other Faker data to complete the User. Time to seed! Run rails db:seed to see what the data is looking like. I am looking at the first user in our collection by typing in pp User.first in rails console.
Success! Everything was populated correctly and I didn’t even have to spend time coming up with a bunch of random data. Thanks Faker!
The Sky is the Limit
One of the best things about Faker is the range of topics you can generate data for. What if you wanted all of your Usernames to be characters from the Ghostbusters franchise?
Or you wanted all your Post’s content to describe the Most Interesting Man in the World?
Take the time to look through Faker’s library to find data that will make your Rails App both easy and fun to work with. Catering your seed data to your interests can really help you focus and stay smiling while you troubleshoot your project. The data is extensive and most of it is downright hilarious so don’t be afraid to take the time to peruse.
Now Go Fake a Cake
With Faker at your fingertips, seeding a database doesn’t have to be a slog. Familiarize yourself with its huge library of topics and revel as the setup for your futures projects becomes a breeze.