[RUBY] [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 seed
How to write Rails
How to uninstall Rails
How to make shaded-jar
How to make a follow function in Rails
[rails] How to post images
Java --How to make JTable
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[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
How to make batch processing with Rails + Heroku configuration
How to make an almost static page with rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to make a Java container
How to use Ruby on Rails
How to make a JDBC driver
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
Rails on Tiles (how to write)
[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
How to return Rails API mode to Rails
How to get along with Rails
[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
How to change app name in rails
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to convert from erb to haml
[Rails] How to upload images using Carrierwave
[Rails] How to use rails console with docker
How to insert a video in Rails