How to Seed Data Fast with the Faker Gem ⚡️???

Table Of Contents

Introduction
What is Faker?
Installation & Usage
Conclusion

Introduction

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 w…


This content originally appeared on DEV Community and was authored by Maxine Meurer

Table Of Contents

Introduction

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, seeding data can be done in a quick, easy, and fun way!

What is Faker?

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.

Installation

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 seeds.rb file, and require the gem at the top of the file.

require 'faker'

You're ready to go, all there's left to do is... Seed. That. Data.

In your seeds.rb file, go ahead and write a small script using the Faker gem.

# 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 .player method was used instead. Most of the generators provide multiple methods for various types of, as well as unique data.

Conclusion

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! ?


This content originally appeared on DEV Community and was authored by Maxine Meurer


Print Share Comment Cite Upload Translate Updates
APA

Maxine Meurer | Sciencx (2021-09-10T18:38:59+00:00) How to Seed Data Fast with the Faker Gem ⚡️???. Retrieved from https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/

MLA
" » How to Seed Data Fast with the Faker Gem ⚡️???." Maxine Meurer | Sciencx - Friday September 10, 2021, https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/
HARVARD
Maxine Meurer | Sciencx Friday September 10, 2021 » How to Seed Data Fast with the Faker Gem ⚡️???., viewed ,<https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/>
VANCOUVER
Maxine Meurer | Sciencx - » How to Seed Data Fast with the Faker Gem ⚡️???. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/
CHICAGO
" » How to Seed Data Fast with the Faker Gem ⚡️???." Maxine Meurer | Sciencx - Accessed . https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/
IEEE
" » How to Seed Data Fast with the Faker Gem ⚡️???." Maxine Meurer | Sciencx [Online]. Available: https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/. [Accessed: ]
rf:citation
» How to Seed Data Fast with the Faker Gem ⚡️??? | Maxine Meurer | Sciencx | https://www.scien.cx/2021/09/10/how-to-seed-data-fast-with-the-faker-gem-%e2%9a%a1%ef%b8%8f%f0%9f%8f%83%f0%9f%8f%bb%f0%9f%92%a8/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.