Create realistic dummy data with gem Faker

What did you do

Carrying out Rails challenges. The challenge is to do something about the output ... Since I graduated from school, a gem Faker that I have never touched came out, so I would like to summarize how to use it.

The execution environment is as follows.

Introduction method

First, check Official Information. According to [Official Note](https://github.com/faker-ruby/faker # note)

――By default, it doesn't always generate unique data, so if you want unique data, add an option. --The master branch may contain data that hasn't been released yet, so check the README for available methods.

It seems that it is. Got it

Installation is very easy, fill in the following in the Gemfile,

Gemfile


gem 'faker'

Complete with bundle install: relaxed:

Try it immediately

The elements that can be generated by Faker are listed in here. I will experiment with various things on the console immediately.

> Faker::Dessert.variety
=> "Upside Down Pineapple Cake"
> Faker::Address.city
=> "Chiba"
> Faker::Address.street_name
=> "Mao"
> Faker::Address.zip
=> "191-3257"
> Faker::Address.street_address
=> "73696 cherry blossoms"
> Faker::App.name
=> "Alpha"

Maybe because the time zone of the system is Tokyo and the default language is Japanese, it also supports Japanese data: relaxed:

Create a Seed file

Then, I will make a Seed file immediately with this.

db/seeds.rb


5.times do
  user = User.create!(
    username: Faker::JapaneseMedia::StudioGhibli.character,
    email: Faker::Internet.email,
    password: 'foobar',
    password_confirmation: 'foobar',
  )
  puts "\"#{user.username}\" has created!"
end

I didn't know how to make password and password_confirmarion the same data, and if a random password is generated, it will be difficult to log in and it will be inconvenient in the development environment, so only username and email Faker I decided to generate it with. It is a hobby to adopt the name of Ghibli character as the name: grin:

Finally, using puts, the name of the output character is written out. As a result of the above, the output character string is as follows.

"Donald Curtis" has created!
"Chihiro Ogino" has created!
"Yupa" has created!
"Yupa" has created!
"Haku" has created!

Oh. Two Yupa-sama have been made. .. .. ..

Create unique data

It is confusing to have two people with the same name, so I rewrote the username part as follows.

db/seeds.rb


#Excerpt
username: Faker::JapaneseMedia::StudioGhibli.unique.character,

With the unique option, unique data will be generated by default. Reset the DB and run rails db: seed again. .. ..

"Traveling Soot" has created!
"Lettie Hatter" has created!
"Jhil" has created!
"Mr. Piccolo" has created!
"Haku" has created!

This time a unique user has been created ^^

Final file

The final file was rewritten as follows. It is easy to see which table data was read.

db  #Show only relevant files
├── seeds
│   ├── users.rb
│   └── posts.rb
└── seeds.rb

db/seeds.rb


require './db/seeds/users'
require './db/seeds/posts'

db/seeds/users.rb


puts 'users ...'
5.times do
  user = User.create!(
    username: Faker::JapaneseMedia::StudioGhibli.unique.character,
    email: Faker::Internet.email,
    password: 'foobar',
    password_confirmation: 'foobar',
  )
end

db/seeds/posts.rb


puts 'posts ...'
User.limit(10).each do |user|
  user.posts.create!(body: Faker::Hacker.say_something_smart,
    image: "https://picsum.photos/350/350/?random")
end

This is the end of implementation ^^

Impressions etc.

Previously, I read this article ("How to write seeds.rb to switch Seed data for each Rails environment") and thought that "It is convenient to write the information of the read data is output". But

The same writing style was used in the curriculum this time, and I was able to recognize that "Oh, this is the commonly used writing style: grinning:". Personally, I like how to write create! instead of create and output the error content: relaxed:

Anyway, I'm glad I met Faker again, which I hadn't touched since I left school: grinning: Now that I have a firm grasp of how to do it, I would like to use it again to generate dummy data: star:

Recommended Posts

Create realistic dummy data with gem Faker
Create dummy data of portfolio with Faker [Note]
Generate dummy data for various tests with Faker (java)
[Rails] Create initial data with seed.rb [Faker] [Japanese localization]
Pokemon Go made with Faker Gem
Create related data together with FactoryBot for yourself
Create an EC site with Rails5 ⑥ ~ seed data input ~
Introduction to RSpec 4. Create test data with Factory Bot
Try create with Trailblazer
Ruby: Create CouchDB data (Create)
[Rails] Prepare dummy data to be used for testing [Faker]