Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 10, 2021 06:38 pm GMT

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

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 users10.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!


Original Link: https://dev.to/mmeurer00/seed-data-fast-with-the-faker-gem-nej

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To