Initial data input with [Rails] seed_fu!

What is seed_fu

seed-fu is a syntax that already exists but you can update only the records you want to change, execute it on a file-by-file basis, and write it easily. It is a convenient gem with sugar.

How to use

  1. Describe the following in the Gemfile.

gem 'seed-fu'

Also execute this.


$ bundle install
  1. Create a directory to put the seed file.

#Must-have directory
db/fixtures

#Create different data for each environment
db/fixtures/development
db/fixtures/production
  1. Create a seed file in db / fixtures below.

01_category.rb



Category.seed do |c|
  c.id = 1
  c.name = 'HTML'
end

Category.seed do |c|
  c.id = 2
  c.name = 'CSS'
end

Category.seed do |c|
  c.id = 3
  c.name = 'Ruby'
end
  1. Execute the seed_fu command.

$ rails db:seed_fu

Tips

Use seed_once

Use seed_once if you don't want to update the data once it's created.


Category.seed_once do |s|
  s.id = 1
  s.name = 'HTML'
end

Syntax sugar

When creating a seed file, you can write as follows in addition to the above description.


Category.seed(:id,
 { id: 1, name: 'HTML' },
 { id: 2, name: 'CSS' },
 { id: 3, name: 'Ruby' },
)

Create data from CSV file

db/fixtures/development/category.csv



1,HTML
2,CSS
3,Ruby

Write the process to read from CSV.

db/fixtures/development/01_category.rb



require 'csv'

csv = CSV.read('db/fixtures/development/category.csv')
csv.each do |category|
 Category.seed do |s|
  s.id = category[0] #Here id
  s.name = category[1] #Here name
 end
end

Finally execute the following command


$ rails db:seed_fu

User created

Since the password is not set properly in seed_fu, use the default seed.

db/fixtures/development/00_user.rb



User.create!(id: 1, name: 'yuh', email: '[email protected]',
password: 'password', password_confirmation: 'password')

Recommended Posts

Initial data input with [Rails] seed_fu!
[Rails] Initial data creation with seed
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] Get standard input for multi-line data
[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
Script to make yaml from CSV to put initial data in Rails with Fixtures
Setup with initial test data inserted in Db2 / DB container
[Rails 6] RuntimeError with $ rails s
Handle devise with Rails
[Rails] Learning with Rails tutorial
[Rails] Development with MySQL
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
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
[Rails] Upload videos with Rails (ActiveStorage)
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Preparation for developing with Rails
Run Rails whenever with docker
[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
[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