Create a large number of records with one command using the Ruby on Rails seeds.rb file

What is seeds.rb?

A file in the app / db directory used to create records.

You don't have to manually register the data one by one when creating a record. Seeds.rb allows you to create a large number of records with a single command.

How to create a record using seeds.rb

  1. Describe the record creation process in the seeds.rb file
  2. Run rails db: seed
  3. Processing of seeds.rb file is executed

Example: Register names and email addresses for 100 people in the users table

Describe as follows in app / db / seeds.rb,

seeds.rb


100.times do |n|
  User.create(name: "name#{n}", email: "mail#{n}@gmail.com")
end

When you run ** rails db: seed **, The users table has name and email values respectively

"name0, [email protected]", "name1, [email protected]", ・ ・ ・ "name99, [email protected]"

100 records are registered as shown in.

Recommended Posts

Create a large number of records with one command using the Ruby on Rails seeds.rb file
Create a Jar file with two lines of command
Try using the query attribute of Ruby on Rails
A note about the seed function of Ruby on Rails
How to create a jar file or war file using the jar command
(Ruby on Rails6) Create a function to edit the posted content
part of the syntax of ruby ​​on rails
Notes on using FCM with Ruby on Rails
Ruby on Rails controller create / delete command
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Ruby on Rails] Create a pie chart for each column with Chartkick
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Ruby On Rails] How to search the contents of params using include?
Perform a large amount of csv export (output) of log information etc. on the WEB application with Rails application
I want to download a file on the Internet using Ruby and save it locally (with caution)
[Ruby on Rails] Until the introduction of RSpec
Publish the app made with ruby on rails
[Ruby on Rails] A memorandum of layout templates
Determine the current page with Ruby on Rails
I checked the number of taxis with Ruby
I made a portfolio with Ruby On Rails
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
A quick note on using jshell with the official Docker image of the JDK
Count the number of occurrences of a string in Ruby
[Introduction] Try to create a Ruby on Rails application
Docker the development environment of Ruby on Rails project
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Docker command to create Rails project with a single blow in environment without Ruby
How to deal with SQLite3 :: BusyException that occurs when uploading a large number of images using ActiveStorage in seeds.rb etc.
[Ruby on Rails] Carousel of bootstrap4 is implemented as a slide show using each method.
[Ruby on Rails] Add a column with a foreign key constraint
(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]
[Ruby on Rails] Easy scroll animation of javascript (using ScrollReveal.js)
A series of steps to create portfolio deliverables with Rails
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Find the number of days in a month with Kotlin
Create a fortune using Ruby
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
The story of the first Rails app refactored with a self-made helper
I want to add a browsing function with ruby on rails
Create a static file that expands variables using the ERB class
Understand code coverage with Rspec, the Ruby on Rails test framework
[Ruby on Rails] Change the save destination of gem refile * Note
I got a warning message with the rails _6.0.3_ new hello_myapp command
[Illustration] Finding the sum of coins with a recursive function [Ruby]
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
[Ruby on Rails] Use the resources method to automatically create routes.
Steps to build a Ruby on Rails development environment with Vagrant
[Ruby on Rails] How to log in with only your name and password using the gem devise
Basic knowledge of Ruby on Rails
Programming with ruby (on the way)
[Java] JUnit that NG if a method with a large number of lines is detected using black magic
Create a Maven project with a command
I ran the rails server command on windows10, Ruby2.6.6 and got "cannot load such file --sqlite3 / sqlite3_native"
[Ruby On Rails] When a model unit test is performed with RSpec using FactoryBot, an error occurs because the foreign key is not entered.
Create a Spring Boot app development project with the cURL + tar command
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Create a Tokyo subway map from the CSV file of station data.jp
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to solve the local environment construction of Ruby on Rails (MAC)!