Populating Your Database Using Faker

Gem Install Faker

As I begin to start building applications for the web, having data to use during development has become more and more important. Testing associations is important at this stage in my journey and having fake data to test against is imperative. Not only is having fake data important to test the functionality of your code, but it is also essential to be able to show off, or present you project in a meaningful way.

Why Fake Data?

After my introduction to ActiveRecord and Sinatra, I wanted to create a basic application that associated employees to companies, where a company has_many employees and an employee belongs_to a company. To test this you would obviously want more than two employees and one company, and having to create employees and companies can become cumbersome rather quickly. Creating your own data for this project would could look something like this, assuming we have our Employee and Company classes built already:

rey = Employee.new(name: "Rey, email: "reyskywalker@jedi.com, address: "2015 Jakku Ave", phone_number: 555-555-5555, title: "Jedi") 

starship_scavengers = Company.new(name: "Starship Scavengers, email: starshipscavengers@jakku.com, address: "1234 Jakku Lane", phone_number: 444-4444-4444, url: "getyourportions.com")

Programmers are supposed to be lazy and doing this for any more than two examples could drive one mad. Imagine having to create dummy data this way for a hundred items.

This is where the faker gem comes in handy.

Installing Faker

Installing faker is as easy as this:

gem install faker

Using Faker

We can play around with faker in irb by doing so:

require 'faker'

For our employees we could generate some fake people by doing this:

name = Faker::Name.name
=> "Kurt Gutmann"
email = Faker::Internet.free_email
=> "kiara_rodriguez@gmail.com"
phone_number = Faker::PhoneNumber.phone_number 
 => "(289) 330-8004"
address = Faker::Address.street_address
=> "31041 Titus Villages"

Employee.new(name: name, email: email, phone_number: phone_number, address: address)

and for our Company it would look something like this:

name = Faker::Company.name 
=> "Stracke-O'Connell"
email = Faker::Internet.free_email
=> "eugene_price@feil.net"
address = Faker::Address.street_address
=> "181 Pete Port"
phone_number = Faker::PhoneNumber.phone_number 
=> "(946) 993-0964"
url = Faker::Internet.url
=> "http://westcorkery.net/alek"

Company.new(name: name, email: email, phone_number: phone_number, address: address, url: url)

I think this is fascinating, but still a lot of typing for two example models.

Using Loops

We can use loops in this case to create bulk data all in one go. Let's give it a try.

50.times do 
   Employee.new(name: Faker::Name.name, email: Faker::Internet.free_email, phone_number:  Faker::PhoneNumber.phone_number, address: Faker::Address.street_address) 
end 

Now if we inspect our Employee class to see how many there are we should see 50.

 Employee.count 
 => 50 

We can do the same thing with our Company class to create however many companies we desire.

Cool, right?

I would say so myself. Faker is a really useful tool that is easy to implement. Faker offers more data types than the ones that I used in this example. This post is intended to show you the power of faker and to help you start populating your databases sooner rather than later. Make sure to check out the Faker Documentation to see what other types of data you can fake.

Show Comments
comments powered by Disqus