DEV Community

Cover image for Using the Faker Gem to Populate your Database

Posted on

Using the Faker Gem to Populate your Database

When building an application, using realistic data is a good idea. But where should you get that data, and how should you add it to your database? There are a few different gems that accomplish this, but Faker is the gem I like and used for a recent Ruby project. Let's walk through how to use it.

What is Faker 🤔

Faker is a Ruby gem that generates fake data that you can use in your database. Per Faker's GitHub page, the gem is a "library for generating fake data such as names, addresses, and phone numbers." You can even use the gem to randomize boolean values, Lorem text, and dates - it's convenient if you need a quick way to get your database going.

To use Faker in your app, follow the instructions in the gem's README, but I found it straightforward. Just install it in your project's folder via your command line like this:

gem install

After installing, you'll see Faker included in your Gemfile, which you can group like I did with the other gems needed to run your app! You're good to go.


Seeding your database 🌱

Adding data to your database is called seeding. I think of seeding as planting data into my empty tables. Remember 👉 creating and defining your database tables is called migration, but those migrated tables start empty, so seeding is how you add data to the tables.

The code used to seed is in its own file called seeds.rb. There is where you write your Ruby methods to plant your Faker data.

I needed Faker data to create my app, Hipster Hire. A fun "job board" that lets users post a job and hire a hipster. I had a ton of fun building this, mainly because the fake hipster data I got was hilarious. A bunch of buzzwords used to make a "hipster bio."? LOL, yes, please.

My app had two Ruby models, a Job model, and a Hipster model - a Job belongs to a Hipster, and a Hipster has many jobs. My database needed fake but realistic job information like title, company name, position, etc., and Hipster information like name and bio. I also wanted to start with some Hipsters already employed and some jobs open and ready to hire a hipster!

Planting your seeds 🪴

It was easy to pick out the Faker data I needed; the gem's GitHub page lets you drill down into the different fake generator categories to see the available methods. I picked the information I wanted and added it to my seed file; more on that in a minute. In GitHub, each Faker line is written, for example, as Faker::Hipster.sentences. This format means that Faker has a static inner class called Hipster that you're extending. And the information after the dot is the specific method called:

Faker::Hipster.sentences #=> ["Godard pitchfork vinegar chillwave everyday 90's whatever.", "Pour-over artisan distillery."]
Enter fullscreen mode Exit fullscreen mode

github faker

From there, how you construct your seed file depends on your table structure and your plan for your app. Once you decide, use the seeds.rb file to add data to the database, all you need to do is write code that uses Active Record methods to create new database records.

In my app. I needed 25 open jobs, so the hipster_id foreign key starts as nil:


I also needed ten hipsters with anywhere from 1-3 jobs. Fortunately, in Ruby, the rand() method accomplishes that - it's a class method that generates a random value between the numbers specified. In this nested method, I'm saying, "generate ten hipsters, and for each hipster created, generate for each of them between 1-3 jobs":


Pretty cool!

Fake it until you make it 🤭

One tip I learned in my coding bootcamp, Flatiron, is to add...

puts "🌱 Seeding..."

[seed code here…]

puts "✅ Done seeding!"
Enter fullscreen mode Exit fullscreen mode the top and bottom of my seeds.rb file because seeding your database doesn't inherently produce anything in your terminal. So to know when the seeding starts and when it's finished, add some simple output like this 👆

The final steps ✅

In my app, I'm using Sinatra, a lightweight framework written in Ruby. It's simple but powerful and flexible, so to populate my database, I ran this command after migrating my tables:

bundle exec rake db:seed

Pro-tip 👉 if I ever need to remove all the data from all my existing tables and rerun the seed command, I have this command:

bundle exec rake db:seed:replant

That's it, and my database tables turned out great! Perfectly randomized fake hipster data. Rad 🤘

PS. this is a snippet of how my Hipster Hire app turned out.

Hipster Hire

Latest comments (0)