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

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


Category.seed do |c| = 1 = 'HTML'

Category.seed do |c| = 2 = 'CSS'

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

$ rails db:seed_fu


Use seed_once

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

Category.seed_once do |s| = 1 = 'HTML'

Syntax sugar

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

 { id: 1, name: 'HTML' },
 { id: 2, name: 'CSS' },
 { id: 3, name: 'Ruby' },

Create data from CSV file



Write the process to read from CSV.


require 'csv'

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

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.


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