Seeding Data using the Faker Gem

Seeding Data using the Faker Gem

How to Easily Quickly .Create Lots Random Data

ยท

5 min read

Introduction

Up to this point, I have desired to have an automated process to "seed" a database with user information. Many times in my early stages of programming, I desired to create applications where users would input information. This information would then be displayed on the application in some way. However, with no data to reference, it was difficult at certain times to visualize what my application would look like or how the data might display. My difficulty was all too evident. I needed to add data to my database, but until I designed a way to input the data, it would be impossible to have the references I desired. I desired to have realistic data in a database which I would not have to enter manually, one by one.

It wasn't until I learned Ruby, and how to use Active Record, that I learned about great options where I could create a realistic database in a matter of seconds.

Set-Up

First, let me show an example from a recent project I created. It was my desire to build a web basic API with Sinatra and Active Record to support a React frontend. My app would allow a leader of a youth group to keep track of fundraising for students attending a group trip. The leader could input a student and each student would have many donors. The app would track how much each donor gave to a particular student and would allow the youth leader to easily see if the donation from a donor was received. A donor could also be deleted or added.

In the back end of my app, I created two models: a student model and a donor model. Each student has many donors. Here is an example of each migration I created:

class CreateDonors < ActiveRecord::Migration[6.1]
  def change
    create_table :donors do |t|
      t.string :first_name
      t.string :last_name
      t.integer :donation
      t.boolean :donation_received
      t.integer :student_id
      t.timestamps
    end
  end
end
class CreateStudents < ActiveRecord::Migration[6.1]
  def change
    create_table :students do |t|
      t.string :first_name
      t.string :last_name
      t.integer :age
      t.timestamps
    end
  end
end

My dilemma, was that I wanted to create many students and a few donors for each student. I understood how to do this using the .create method like so:

Donor.create(first_name: "Joe", last_name: "Smith", donation: 25, donation_recieved: false, student_id: 1)
Student.create(first_name: "Joe", last_name: "Smith", age: 25)

I could do this in the seeds.rb file for ever single donor and every single student, but quite frankly I did not have the time. More importantly, the data would not be unique to each donor or student because copy and paste would have been my next solution.

Thankfully, I found a gem to create randomized data for me while I was busy learning at Flatiron School.

The Faker Gem

The faker gem became my saving grace. Not only would it seed as much data as I desired, it would allow me to create random data. The amount of data generators I could choose from were unreal! For example, Faker can create random addresses, names, video games, military ranks, and so much more! Check out their resource to find out all they offer. However, let me show you how I used this gem to easily seed data into my project.

Add the gem to Your Gemfile

First, you must add the gem to your Gemfile. You can easily get access to the most current Faker gem by adding the following to your Gemfile:

gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'

If you want access to all the Faker generators, including the unreleased generators, you will need to use:

gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main'

Make sure you install the gem

Using bundle install you can install all the gems in your gem file.

Use the generators in a seed file

Using the list of generators provided by Faker, you can now choose which generators you might need to seed data to your database. In the seed file, you simply need to add a generator like Faker::Name for each input like so:

puts "๐ŸŒฑ Seeding data..."

# Seed your database here

8.times do

  #Create a list of 8 students 
  student = Student.create(
    first_name: Faker::Name.first_name,
    last_name: Faker::Name.last_name,
    age: rand(12..18)
  )

  #Create 1-10 donors for each student
  rand(1..5).times do
    Donor.create(
      first_name: Faker::Name.first_name,
      last_name: Faker::Name.last_name,
      donation: rand(10..100),
      donation_received: Faker::Boolean.boolean,
      student_id: student.id
    )
  end
end

puts "โœ… Done seeding!"

For my project, I asked Ruby to run a loop for eight times. Each time the loop was ran, I desired for a student to be created. For each student, I desired to create a donor one to five times. Each student was given a random first name from Faker and a random last name. You can see this by the Faker::Name.first_name and Faker::Name.last_name generators I used. The .first_name allowed me to generate names which were strictly first names.

Seeding the Data

Just like with manually entering data, you can simply use bundle exec rake db:seed in the console to seed your database.

Wrapping Up

Overall, I would highly recommend using Faker to help seed your database. Especially if you need to create a lot of data fast. By using the Faker gem in your developer environment, it will allow you to have a much more successful launch of your application. I hope this helps you, as much as it did me!

Resources

Faker Gem

Did you find this article valuable?

Support Jerry Fitzner by becoming a sponsor. Any amount is appreciated!

ย