Fake It While You Make It.

by Joseph Patterson

Here We Go Again

What is Faker?

Installation

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

Can I get a zoom zoom?

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.

Hmmmm. I wonder which category has what I need?

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.

Hey! It is my homey Stephane Harber!

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

Who you gonna call?

Or you wanted all your Post’s content to describe the Most Interesting Man in the World?

Women want him and men want to be him.

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

I love traveling, music, and movies.