Faker Gem으로 데이터를 빠르게 시드하는 방법 ⚡️🏃🏻💨
목차
소개
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 , 데이터 시드를 빠르고 쉽고 재미있게 할 수 있습니다!페이커란?
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 .설치
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.
결론
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! 🌱
Reference
이 문제에 관하여(Faker Gem으로 데이터를 빠르게 시드하는 방법 ⚡️🏃🏻💨), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/maxinejs/seed-data-fast-with-the-faker-gem-nej텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)