LoginSignup
0
0

More than 1 year has passed since last update.

Railsにサンプルデータを作成する方法

Posted at

1,Fakerを導入する

gem 'faker'

今回私はtest環境とdevelopment環境に反映したいので、

group :development, :test do
  # (略)
  gem 'faker'
end

2、db/seed.rbにサンプルデータを作成する処理を書く。

# 追加のユーザーをまとめて生成する
99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name:  name,
               email: email,
               password:              password,
               password_confirmation: password)
end

3,rails db:seedコマンドでデータベースにサンプルデータを反映。

必要ならばrails db:migrate:resetコマンドで、いまあるDBのデータを全て削除できる。
以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0