How to Seed Data Fast with the Faker Gem ⚡️🏃🏻💨
Tags: ruby, javascript, database, devops
Published: 2021-09-10 18:38:59

## Table Of Contents
  * [Introduction](#1)
  * [What is Faker?](#2)
  * [Installation & Usage](#3)
  * [Conclusion](#4)

### Introduction <a name="1"></a>

Chances are you're here because you saw the word combination *Seed Data Fast*, and I don't blame you! Creating a database is enough work itself, so coming up with custom seed data can become an unnecessary and time-consuming task. But all thanks to the Ruby **[Faker gem](https://github.com/faker-ruby/faker)**, seeding data can be done in a **quick**, **easy**, and **fun** way!

### What is Faker? <a name="2"></a>

Faker is a Ruby gem written by Jason Kohles. Like many of us, Jason got sick of spending time writing out seed data, so he made a gem to make all of our lives easier. *Thanks, Jason!* Faker comes with a handful of generators that allow you to generate fake data such as names, emails, phone numbers, addresses, Twitter posts, job titles, and more! There are also methods available to provide you with [unique data](https://github.com/faker-ruby/faker#ensuring-unique-values). 

### Installation <a name="3"></a>

*This is a Ruby Gem and will only work for Ruby applications.*

First, install the Ruby Faker Gem.

```
gem install faker
```

Once the gem has successfully installed, head over to the <code>seeds.rb</code> file, and require the gem at the top of the file.

```ruby
require 'faker'
```
You're ready to go, all there's left to do is... *Seed*. *That*. *Data*.

In your <code>seeds.rb</code> file, go ahead and write a small script using the Faker gem.
```ruby
# generate 10 users
10.each do
    username = Faker::Esport.player
    name = Faker::Name.unique.name
    profession = Faker::Job.title
    email = Faker::Internet.unique.email
    address = Faker::Address.full_address
    phone = Faker::PhoneNumber.unique.cell_phone
    User.create(username: username, name: name, email: email, profession: profession, address: address, phone: phone )
end
```
Once you've created a beautiful script containing all your lovely data, seed it! In your terminal run:

```
rails db:seed
```
You can check everything was seeded correctly by confirming your data is present within the rails console, or if you have your server up and running, you can check your routes. 

*Note: If no seed data shows up, see that you are meeting all validations in your model that may be prohibiting the data from being created in the first place.*

There you have it! ✨*Data*✨

If you need to create data that there are not necessarily generators for, get creative with ones that already exist! As you can see in the example script provided above, there was no username generator, so the Esport generator with the <code>.player</code> method was used instead. Most of the generators provide multiple methods for various types of, as well as unique data. 

### Conclusion <a name="4"></a>
    
Creating seed data can be a tedious task, but it doesn't have to be! The Faker gem is fantastic for fast, simple, and sometimes funny seed data. 

If you have any alternative ways/gems to seed data, feel free to share them below! Happy Seeding! 🌱