[Rails] Initial data creation with seed

Initial data creation with seed

table of contents

  1. In creating initial data
  2. Create seed file

1. In creating initial data

We have decided to add administrator privileges to the user function. To create an admin user, I created an admin user from rails c. I would like other development members to create an administrator user, but considering the possibility of creating each from rails c, it will be troublesome or forgotten, so seed I decided to create a file and have an admin user created.

2. Create seed file

This time, we will create the data of the administrator user.

under db db/seeds/admin_user.Create an rb.


 Create a seed file as shown below.


#### **`db/seeds/admin_user.rb`**
```rb

User.create(admin: 1, email: "[email protected]", password: "123456")

Enter the following command in the terminal.

bundle exec rails r db/seeds/admin_user.rb

If you check the database, an admin user has been created.

You can also easily create multiple data by using the each statement in the seed file.

This is the initial data creation with seed.

Recommended Posts

[Rails] Initial data creation with seed
Initial data input with [Rails] seed_fu!
API creation with Rails + GraphQL
Create an EC site with Rails5 ⑥ ~ seed data input ~
Rails6: Input the initial data of ActionText using seed
[Rails] Create initial data with seed.rb [Faker] [Japanese localization]
[Ruby on Rails] Introduction of initial data
[Rails] Manage data for each seed file
[Rails] Many-to-many creation
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
Start SQL Server with Docker & register initial data
Launch Docker image with initial data injected with docker-compose
Script to make yaml from CSV to put initial data in Rails with Fixtures
Setup with initial test data inserted in Db2 / DB container
CLI creation with thor
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
Handle devise with Rails
Graph creation with JFreeChart
[Rails] Test with RSpec
[Rails] Development with MySQL
[Rails] Initial setting of user-created login with devise, devise_token_auth
Supports multilingualization with Rails!
Double polymorphic with Rails
Summary of initial work when creating an app with Rails
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
Java multi-project creation with Gradle
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
Portfolio creation Ruby on Rails
[Rails] Upload videos with Rails (ActiveStorage)
Try using view_component with rails
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Japaneseize using i18n with Rails
Preparation for developing with Rails
Run Rails whenever with docker
Rails reference type creation added
[Docker] Rails 5.2 environment construction with docker
Use multiple databases with Rails 6.0
[Rails] Specify format with link_to
Login function implementation with rails
How to write Rails seed
[Rails] New app creation --Notes--
[Rails] How to make seed
[Docker] Use whenever with Docker + Rails
Build apache7.4 + mysql8 environment with Docker (with initial data) (your own memo)
[Rails] I want to add data to Params when transitioning with link_to