DEV Community

Cover image for How-To: Using the Faker Gem to Seed your Rails Database
Bryan Oyloe
Bryan Oyloe

Posted on

How-To: Using the Faker Gem to Seed your Rails Database

Introduction

Faker is a ruby gem that gives you random data in many different categories that can easily generate database seeds for use in your rails applications. Say you are building an app that tracks the Pokemon that a trainer has captured. In order to test your database, you would need to seed it with some Pokemon and trainers. Using Faker gem you can generate random trainers with names and ages, as well as randomly produce the Pokemon that will seed your database using the names and attributes of the actual Pokemon from the game. No need to program each one in yourself!

How to Use

Installation

Installation of the faker gem is very straightforward. The Faker README is very detailed and provides a great deal of information. To install the faker gem simply type gem install faker in your terminal. After the gem installation is complete, open your rails app and go to your seeds file to begin using Faker. How you utilize the gem depends on the type of data you will be looking for. The README lists all of the different classes that Faker supports, such as Name, Address, Artist etc.
If you prefer to display the list of methods straight from your terminal, there is another gem called faker-bot that can be installed. This guide can walk you through the install process easily.

Application

I started using the Faker gem during a weekend "build and burn" Rails app session with 3 of my class mates at Flatiron School. We were practicing making a one:many relationship and had decided on a players to sports teams model. Here is how our database was set up:

ActiveRecord::Schema.define(version: 2020_08_30_213624) do

  create_table "players", force: :cascade do |t|
    t.string "name"
    t.integer "number"
    t.integer "team_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["team_id"], name: "index_players_on_team_id"
  end

  create_table "teams", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  add_foreign_key "players", "teams"
end
Enter fullscreen mode Exit fullscreen mode

Rather than tediously typing out a bunch of different player's names and numbers, creating a bunch of team's names to assign to the players , I used the Faker gem to do the work for us. Faker::Sports::Basketball allowed us to automatically populate our seed data like so:

Player.destroy_all
Team.destroy_all

5.times do 
    Team.create(name:Faker::Sports::Basketball.team)
end

teams = Team.all 

10.times do 
    Player.create(name: Faker::Sports::Basketball.player, 
                  number: rand(1..99),  
                  team: teams[rand(0..4)])
end
Enter fullscreen mode Exit fullscreen mode

In the Team.create we used the Basketball.team method to randomly generate an actual NBA team using data from Faker. We then populated the array teams by calling Team.all.

Once the teams were created, we used Basketball.player to generate a random, actual NBA player's name. We then randomly assign them a jersey number, and a team by calling team:teams[rand(0..4)]. After running rails db:seed we get a teams table that looks like this:

[#<Team:0x00007fef603eaf88
  id: 12,
  name: "Chicago Bulls",
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Team:0x00007fef5ca7aa28
  id: 13,
  name: "Golden State Warriors",
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Team:0x00007fef5ca7a0f0
  id: 14,
  name: "Toronto Raptors",
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Team:0x00007fef5ca79fb0
  id: 15,
  name: "Memphis Grizzlies",
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Team:0x00007fef5ca79cb8
  id: 16,
  name: "Los Angeles Clippers",
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>]
Enter fullscreen mode Exit fullscreen mode

and we get a players table with this data:

[#<Player:0x00007fef604c3748
  id: 12,
  name: "Kevin Durant",
  number: 99,
  team_id: 16,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
#<Player:0x00007fef60501d68
  id: 13,
  name: "Stephen Curry",
  number: 14,
  team_id: 13,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501c78
  id: 14,
  name: "Kyrie Irving",
  number: 41,
  team_id: 13,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501bb0
  id: 15,
  name: "LeBron James",
  number: 26,
  team_id: 12,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501a70
  id: 16,
  name: "Anthony Davis",
  number: 76,
  team_id: 12,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501908
  id: 17,
  name: "Nikola Jokić",
  number: 35,
  team_id: 15,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef605017f0
  id: 18,
  name: "Kyle Lowry",
  number: 92,
  team_id: 16,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501700
  id: 19,
  name: "Dirk Nowitzki",
  number: 31,
  team_id: 16,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef60501638
  id: 20,
  name: "Russell Westbrook",
  number: 84,
  team_id: 14,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>,
 #<Player:0x00007fef605014a8
  id: 21,
  name: "Karl-Anthony Towns",
  number: 83,
  team_id: 15,
  created_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00,
  updated_at: Fri, 04 Sep 2020 22:30:41 UTC +00:00>]
Enter fullscreen mode Exit fullscreen mode

Using this method, we were able to generate 5 teams and 10 players assigned to those teams in a few simple lines of code, instead of typing out each team and player by hand.

Conclusion

One caveat to be aware of, the Faker gem generates random data but doesn't guarantee uniqueness. If your data seeds require uniqueness, check this out for more direction.

Faker is a cool and fast way to generate bulk data quickly for your Rails app seeds. Hope this helped you learn more about it. Thank you for reading.

Top comments (0)