[Ruby on Rails] Introduction of initial data

Introduction

By inputting the initial data for operation check, If you reset the DB, you can recreate the initial data based on the seed file, Because you can create a large amount of data at once This is a necessary function for applications in the development stage.

This time, edit the seed file and input the initial data.

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

What is a seed file?

The file created when the rails new 〇〇 command is executed is directly under the db directory. db/seeds.rb

Actually create

db/seeds.rb


# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
#   Character.create(name: 'Luke', movie: movies.first)
User.create!(
   email: '[email protected]',
   password: 'testpass',
)
Post.create!(
   title: 'Thank you',
   content: 'About seed files',
)

Reflected in database

Terminal


$ rails db:seed

Now that the user can log in, the Post is populated with data. After that, as a confirmation method, execute as follows If the data is included, it is successful.

Terminal


$ rails c
pry(main)> Post.all

When creating multiple files

The for statement and the content are in, and the statement processing is repeated the specified number of times. The initial number starts from 0, so in the following cases it will be 0-9.

db/seeds.rb


  Post.create!(
    title: 'Thank you',
    body: 'About seed files'
  )

  10.times do |number|
    List.create!(title: 'About times',body: number)
  end

Summary

There are other ways to input data from csv, so If you are interested, please check it out.

Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork

Recommended Posts

[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Until the introduction of RSpec
Basic knowledge of Ruby on Rails
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
[Rails] Introduction of PAY.JP
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Implementation of Ruby on Rails login function (Session)
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] Individual display of error messages
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Introduction] Try to create a Ruby on Rails application
Ruby on rails learning record -2020.10.03
[Rails] Introduction of devise Basics
Portfolio creation Ruby on Rails
Implementation of Ruby on Rails login function (devise edition)
Ruby on rails learning record -2020.10.04
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
[Ruby on Rails] Implementation of tagging function/tag filtering function
Ruby on rails learning record-2020.10.07 ②
Rails6: Input the initial data of ActionText using seed
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Try using the query attribute of Ruby on Rails
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
[Rails] Introduction of Rubocop by beginners