Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display

Building a bulletin board API with authentication authorization in Rails 6 # 13 Adding authentication header

Completion drawing

$ rails db:seed 
-----user start-----
count from: 0
count to: 10
3.4542s
-----user end-----
-----post start-----
count from: 0
count to: 13
0.1095s
-----post end-----
3.5883s

Edit seeds file

First of all, as with the modification of controller, post is generated from user, so we will create it from user_seeds.rb.

db/seeds/user_seeds.rb


# frozen_string_literal: true

unless User.exists?
  10.times do |i|
    email = "test#{i + 1}@example.com"
    User.create!(email: email, password: "password",
                 uid: email, provider: "email", name: Faker::Name.name)
  end
end

For the time being, let's generate 10 almost users.

Next, generate post_seeds.rb so that it belongs to the user created above.

db/seeds/post_seeds.rb


# frozen_string_literal: true

unless Post.exists?
  users = User.all
  users.each do |user|
    Random.rand(0..3).times do
      user.posts.create!(subject: Faker::Lorem.word, body: Faker::Lorem.paragraph)
    end
  end
end

Get all users and randomly generate 0 to 3 posts. It's a good idea to remember Random.rand (x..x) as it is a technique often used in seed.

Editing db / seeds.rb

db/seeds.rb


# frozen_string_literal: true

seed_models = %i[user post]

all_process_time = Benchmark.realtime do
  seed_models.each do |model|
    puts "-----#{model} start-----"
    puts "count from: #{model.to_s.classify.constantize.count}"
    process_time = Benchmark.realtime do
      require "./db/seeds/#{model}_seeds"
    end
    puts "count to: #{model.to_s.classify.constantize.count}"
    puts "#{format('%.4<time>f', time: process_time)}s"
    puts "-----#{model} end-----"
  end
end
puts "#{format('%.4<time>f', time: all_process_time)}s"
$ rails db:seed 
-----user start-----
count from: 0
count to: 10
3.4542s
-----user end-----
-----post start-----
count from: 0
count to: 13
0.1095s
-----post end-----
3.5883s

As the number of models increases and the processing time increases, it should be easier to investigate where the bottleneck is.

Continued

Building a bulletin board API with authentication authorization with Rails 6 # 15 pundit introduction [To the serial table of contents]

Recommended Posts

Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails 6 # 11 User model test and validation added
Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
Building a bulletin board API with authentication authorization with Rails 6 Validation and test implementation of # 4 post
I tried to make a group function (bulletin board) with Rails
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
Create a simple bulletin board with Java + MySQL
Try to create a bulletin board in Java
Display a confirmation screen before registering a record with rails
[Note] Build a Python3 environment with Docker in EC2
Build Rails (API) x MySQL x Nuxt.js environment with Docker