Faker Gem: Fake Data Generation in Ruby.

3 min readOct 16, 2020


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.

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

File name: Gemfile

Run the command ‘bundle install’ as always.

Just to confirm and see if the gems installed successfully.

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

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.

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

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
Faker::Internet.email → email
Faker::PhoneNumber.phone_number → phone_number
Faker::Company.name → 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

And here is the final step for data generation:

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

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

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:

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!




Ruby on Rails Development Company