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,


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

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 the command
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
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
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
[Introduction] Try to create a Ruby on Rails application
Docker command to create Rails project with a single blow in environment without Ruby
[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
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
[Illustration] Finding the sum of coins with a recursive function [Ruby]
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
Create a Tokyo subway map from the CSV file of station
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)!
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Ruby on Rails When you don't know the cause of rollback when saving.
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] View test with RSpec
[Rails6] Create a new app with Rails [Beginner]
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] model, controller terminal command
Ruby on Rails model creation / deletion command