Using the Faker Gem to Populate your Database
Tags: ruby, beginners, tutorial, codenewbie
Published: 2022-11-01 21:23:02

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](https://github.com/faker-ruby/faker), 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](https://github.com/faker-ruby/faker), but I found it straightforward. Just install it in your project's folder via your command line like this:


![gem install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lss56i097gvui36plalk.png)



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.

![gemfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtowlc2sivweasldcnmx.png)


#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."]
```


![github faker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsa6ddesqq9z84v14uho.png)


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`:

![Job](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxl80z6wstjnl6yt3qat.png)


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":

![Hipster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cps69ji5fvj0et0mb1h4.png)

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!"
```


...at 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](https://i.makeagif.com/media/11-01-2022/LOLAoc.gif)