Ruby Gems: Seeding your database with Faker

Matthew Sedlacek
3 min readMay 12, 2020

--

Photo by Matt Briney on Unsplash

In developing your next Ruby application, there will be a point in which you need data to see if your program is running the way in which it was designed. If an API(Application programming interface) or data set is not readily available, Ruby’s Faker Gem can be a viable replacement for testing purposes.

Please see below for a step-by-step tutorial on the Faker Gem from installation to implementation.

1. Install the Gem

To find and search for Ruby Gems visit https://rubygems.org/. Pictured below is the documentation provided for the Faker Gem as well as what you will need to copy and paste into your program to successfully install the Gem.

Faker Gem on https://rubygems.org/

After you have located the Faker Gem site copy and paste the provided information under GEMFILE into into your personal Gemfile.

Input Faker Gem into your Gemfile

After pasting the Faker Gem into your Gemfile you will need to require ‘faker’ in your personal environment.rb file.

Require Faker in your environment folder

The last step is installing the gem in your terminal. You can either type “gem install faker” or copy and paste the provided text from the Faker page on ruby.org . If you completed the above steps correctly you will receive the below message.

Gem Installation Complete

2. When to use the Faker Gem?

The Faker Gem’s purpose is to provide fake data to our database. Therefore, it should only be used after your models and migrations are complete and you have the desired schema for your database tables.

3. Types of Data Faker can provide

Faker provides lists of fake data categories that range from traditional(names, phone numbers, and addresses) to outlandish (superheros, hipster, and tv shows). To see a complete list visit https://github.com/faker-ruby/faker.

Examples of data under the Faker::Beer category https://github.com/faker-ruby/faker

4. Implementation

To properly seed your database you will need a a personal seeds.rb file. Within the seeds file you will input any Faker data you wish to use as well as any other data you will need to fill-out your database tables. The command to run your seed file and populate your database is rake db:seed.

In a recent group project, we used Faker to generate many different portions of our database. Below you can see Faker::Beer, Faker::Hipster, and Faker::Twitter being used to create data points.

Example Seeds.rb file

Tips/Warnings:

-Not all data provided by the Faker Gem is guaranteed to be unique. For example, Faker could create duplicate phone numbers or names in your dataset.

-Make sure to include a destroy_all at the top of your seed file. If you don’t and are seeding and dropping individual tables your ids will not start at 1.

-When you want a final clean dataset set run all your data creation at one time using rake db:reset. If you are using the reset command make sure that you list your destroy_all in order from most contingencies to least. In the example above, Review is a joiner table, Beer is a child table, and User/Brewery are parent tables.

--

--

Matthew Sedlacek
Matthew Sedlacek

Written by Matthew Sedlacek

Software Engineer — Full Stack, JavaScript, ReactJS, Ruby on Rails, OO Programming (https://www.matthewsedlacek.com/)

No responses yet