Create dummy data of portfolio with Faker [Note]

It is very troublesome for beginner engineers to manually enter the dummy data of the portfolio, and I want to enter the dummy data in a cool way. It's easy to do with gem Faker.

Implementation flow

The working time will be about 30 minutes for the first time.

  1. Introduction of gem Faker
  2. Japanese localization of faker
  3. bundle install from the terminal
  4. Described in the seed file
  5. Create dummy data / rails db: seed
  6. Check if it is reflected in the DB

Let's do it!

1. Introduction of gem faker

First, let's introduce Faker to Gemfile!

Gemfile


gem 'faker'

2. Japanese localization of faker

Next, install the Japanese localization file of faker. Download or copy ja.yml from the link below, place it in config> locales> ja.yml in rails. https://github.com/faker-ruby/faker/blob/master/lib/locales/ja.yml キャプチャ.PNG Next, write `` `config.i18n.default_locale =: ja``` in the module of application.rb.

application.rb


module hoge
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
    config.i18n.default_locale = :ja #Add here
  end
end

3.bundle install Let's bundle install in the terminal!

4. Described in the seed file

Finally, we will put the test data in the seed file. In the following case, 50 random dummy data will be input to the name / character / email column created by model. Check READE.me for the method description of Faker ::!

seeds.rb


50.times do 
  User.create(
    name: Faker::Name.name, 
    character: Faker::Games::Pokemon.name, 
    email: Faker::Internet.email,
  )
end
#Leftmost name:Is the model column name

Next, use rails db: seed to reflect the data.

5. Check if it is reflected in the DB

Let's check if it is reflected in `` `rails c``` from the console.

User.all

If the data in Japanese is included, it is complete. Thank you for your hard work.

Recommended Posts

Create dummy data of portfolio with Faker [Note]
Create realistic dummy data with gem Faker
A series of steps to create portfolio deliverables with Rails
Create portfolio with rails + postgres sql
Data management of owned books (portfolio)
Create related data together with FactoryBot for yourself
Create a Jar file with two lines of command
Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)
Create an EC site with Rails5 ⑥ ~ seed data input ~
Create Restapi with Spring Boot ((1) Until Run of App)
[Note] Create a java environment from scratch with docker
Introduction to RSpec 4. Create test data with Factory Bot