LoginSignup
0
0

More than 1 year has passed since last update.

Fakerを利用してダミーデーター作成

Posted at

使う意味

掲示板などを作成して投稿されたらどのようになるのか、確かめたい時に一個一個手作業で書くよりダミーデーターを作成して確認するため。

Gemのインストール

Gemfile

gem 'faker'

そしてbundle install

seedファイル編集

db/seeds.rb

20.times do     
    User.create!(
        first_name: Faker::Name.first_name, 
        last_name: Faker::Name.last_name,
        email: Faker::Internet.email,
        password: '123qwe',
        password_confirmation: '123qwe'
    )
end

と私の場合このように記載する
20.times doで20個作成する

first_name: Faker::Name.first_nameはfirst_nameにFakerのNameの中のfirst_nameメゾットを使うという意味

なのでpasswordに関してはfakerのメゾットは使わず20個全部のpasswordを123qweにしている。

createだとtrue/falseしか返しません。これだとuserの作成に失敗した場合に、エラーが返ってきません。なので、create! を使います。

,はお忘れずに

最後に

ターミナルで

rails db:seed
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