DEV Community

Cover image for How to Seed Data Fast with the Faker Gem βš‘οΈπŸƒπŸ»πŸ’¨
Maxine
Maxine

Posted on • Updated on

How to Seed Data Fast with the Faker Gem βš‘οΈπŸƒπŸ»πŸ’¨

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Once you've created a beautiful script containing all your lovely data, seed it! In your terminal run:

rails db:seed
Enter fullscreen mode Exit fullscreen mode

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

Top comments (4)

Collapse
 
superails profile image
Yaroslav Shmarov • Edited

good syntax!

10.each do
    username = Faker::Esport.player
    User.create(username: username)
end
Enter fullscreen mode Exit fullscreen mode

is better than

10.each do
    User.create(username: Faker::Esport.player)
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ben profile image
Ben Halpern

Love the faker gem

Collapse
 
victorocna profile image
Victor Ocnarescu

We also have Faker in the Javascript world. It's my go to package whenever I need to fake some data locally.

Collapse
 
maxinejs profile image
Maxine • Edited

Omg, thank you for sharing, this is awesome! Looks like this article will be needing an update.