Toll free (US) : +1 (888) 838-3532 | NYC: +1 (646) 491-6866

Gems are libraries in Rails that generally enable you to write the application code faster and thereby making a great product in far lesser time. Usually, whenever we start developing any application, there comes a point when we need data which we can use to see how the application will behave while doing some load testing or how it would look when we deploy it to the production. The manual process of creating the data can be daunting. Faker gem serves to take this pain away by generating the fake data just as needed and saving us all the time and effort otherwise wasted in the manual process of data-generation.

It can generate almost any kind of data suitable for our application. For example, it can generate the fake data for fields such as name, email, passwords, phone-numbers, paragraphs, etc. It is therefore, an awesome way of populating the model (which is a database layer in Rails)

Let’s take a look at this gem by creating a sample project.
Faker Gem

Open the Gemfile from application directory and add ‘gem faker’ as shown below.

File name: Gemfile

faker gem 2

Run the command ‘bundle install’ as always.

faker gem 3

Just to confirm and see if the gems installed successfully.

faker gem 3

So, the installation was successful! Now, let’s create a model for which we will do the data
population using faker gem. We will call our sample model as PersonDetail & it will have the following fields:

1. First Name
2. Last Name
3. Age
4. Email
5. Phone Number
6. Company Info

faker gem 4

As we have used the scaffolding for faster demonstration, the table with name ‘’person_details’’ with mentioned fields will be created after migrating the database.

faker gem 5

At this point, we should have the table created with the mentioned fields. We can check the actual structure of this table in schema.rb file

File name : db/schema.rb

faker gem 6

Now, there is a file called seeds.rb present in application directories ‘db’ folder. There are
Some examples already available (the commented part) in this file that demonstrates how to make use of this file.
We can also go ahead on the same lines and try to create data for defined fields in “person_details” table by using Faker gem.


We have some common fields such as FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, etc. in our table. For these fields, the usage is, as shown below:

Faker::Name.first_name                         →    first_name
Faker::Name.last_name                         →    last_name
Faker::Number.number(2)                      →    age                              →    email
Faker::PhoneNumber.phone_number    →    phone_number                           →    company_info

All these values will be generated in random ways to achieve considerable uniqueness in the data generated.
Combining all of these fields, we can make it to create as many number of rows as we want in person_details table.

File name : db/seeds.rb

faker gem 7

And here is the final step for data generation:

faker gem 8

We can start the server as “rails server” and enter the route as

gem 1

There we go! We can see that 5 distinct rows are created and shown on the index listing page:

gem 2

Now, while Faker generates the information in random ways, the data generated by default may or may not be completely unique all the time. To ensure that the data generated is unique at all times, we can make use of ‘unique’ just before the actual field call:

gem 3

There are many such fields for which we can have fake data. You can find an exhaustive list of the same here:

Stay tuned for more!


Pin It on Pinterest

Share This