[Rails] How to make seed

Purpose

I will output what I learned by studying seed! I hope it will be helpful for those who are learning seed from now on.

What is seed

The seed file is the initial data. For example, if you reset the database during development, all the data will be lost. If it disappears every time, if there is a registration function, it will be necessary to re-register the data every time it is reset. It's really troublesome, isn't it? Therefore, if you describe the data you want to put in the seed file in db / seeds.rb, you do not have to recreate it one by one! !! !!

Basic pattern

This will create one seed data for User.

User.create!(email: "[email protected]",password: "password" )

When you want to create multiple seed data

It uses basic Ruby commands! I'm passing n as an argument so that the emails aren't the same. The reason for setting it to n + 1 is that if it is only n, the data will start from 0.

10.times do |n|
 User.create!(
   email: "user#{n+1}@example.com",
   password: "password" )
end

About creating association data

For example, suppose you have a User and its associated Task model. As seed data, we will generate a Task related to the User model. Then you can write as below!

User.all.each do |user|
 Task.create!(
  user_id: user.id,
  title: "title",
  memo: "memo",
  color: "red",
  start_date: "2020/5/1",
  end_date: "2020/5/30" )
end

How to read CSV file

To read the CSV file, write as follows. The "db / csv / masters / init_categories.csv" part changes depending on where you put the file.

CSV.foreach("db/csv/masters/init_categories.csv") do |row|
 @categories = Masters::Category.create!(name: row[0])
end

About image insertion

This time, I prepared a sample image under the application, so I will describe how to do it below. Load using the open method.

User.create!(
    image: open("db/images/sample.png "),
    title: "topic",
    overview: "Overview",
    link: "http://origin_job_topic_sample.com" )

When you want to enter a value at random

For example, when the User model is described as below, when creating seed data, you want a pattern that contains both, right? By the way, enum is a function that allows you to store users and admins numerically.

enum user_type: {
    user: 0,
    admin: 1,
  }

In such a case, if you write as follows, the data will be created randomly! This also uses the basic writing style of Ruby!

User.create!(
 email: "[email protected]",
 password: "password",
 status: rand(0..1) )

Summary

I hadn't touched seed before I changed jobs, but I think I learned the basics this time. I'm still learning, so I'll update it as soon as I get new knowledge! !!

Recommended Posts

[Rails] How to make seed
How to write Rails
How to uninstall Rails
How to make shaded-jar
[rails] How to post images
[Rails] How to use enum
[Rails] How to use enum
How to read rails routes
How to use rails join
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
[Rails] How to write exception handling?
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to make a splash screen
How to make a Jenkins plugin
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to make a Maven project
[Introduction to Rails] How to use render
How to make a Java array
[Android] How to make Dialog Fragment
How to install Swiper in Rails
How to make a Java calendar Summary
How to implement search functionality in Rails
[Rails] How to upload images using Carrierwave
How to insert a video in Rails
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
[Rails] How to display error messages individually
How to use MySQL in Rails tutorial
How to resolve errors when installing Rails 5.1.3
How to make a Discord bot (Java)
[Ruby on Rails] How to use redirect_to
[rails] How to create a partial template
[Rails 5.x] How to introduce free fonts
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
How to implement image posting using rails
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
How to build Rails 6 environment with Docker
How to make the schema of the URL generated by Rails URL helper https
[Rails] How to decide the destination by "rails routes"