LoginSignup
3
3

More than 5 years have passed since last update.

Rails + faker でダミーデータ作成

Last updated at Posted at 2017-10-07

Railsで学習する時にダミーデータが必要かなと思った調べたのでまとめます。

環境

  • CentOS6.5
  • Rails 5.1.4
  • MySQL 5.7

Faker取得

Gemfileに

gem 'faker'

を追記し bundle installする。

$ gem list | grep faker
faker (1.8.4)

でfakerが入っていることを確認する。

RailsでMySQLのテーブル作成で作成したテーブルを使います。

seed.rbにダミーデータ作成処理を作る

$ vi db/seed.rb

↓をコピペする

100.times do
     User.create(
        name: Faker::Name.first_name,
        uuid: Faker::Internet.password,
        age: rand(0..100)
      )
end

これは、Usersテーブルに100個ダミーデータを作成しますという処理になります。

テーブルに反映する

$ rails db:seed

これで完了です。

3
3
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
3
3