7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ruby on Rails】Faker(ダミーデータ)を使ってデータベースに保存する方法

7
Posted at

はじめに

前回、gem Fakerの基本と日本語化について執筆しました

今回はその続きになります、実際に生成したデータを保存するところまで行います。
間違ってたらご指摘ください!

前提条件

Ruby on Rails 7.2.3
Ruby 3.4.8
Docker環境
postgres:17.7

また、マイグレーションファイルを用意しました。

class CreateProfile < ActiveRecord::Migration[7.2]
  def change
    create_table :profiles do |t|
      t.string :name
      t.integer :age
      t.string :pokemon
      t.timestamps
    end
  end
end

今回生成するのはプロフィールテーブルの

  • 名前
  • 年齢
  • 好きなポケモン
    です。

前回はrailsコンソールでデータを生成しましたが、今回は実際にファイルに記述して保存することろまで実施します。

ファイル記述からデータ生成まで

ファイルにFakerを記述していきます。
編集場所:db/seeds.rb

Profile.create!(name: Faker::Name.first_name,
                age: Faker::Number.between(from: 1, to: 100),
                pokemon: Faker::Games::Pokemon.name
)

以上です。
(create!メソッドを使うと、保存に失敗したときエラーを表示してくれるのでデバッグしやすいです)

ターミナルでファイルを実行するコマンドを入力しましょう。
実行場所:ターミナル

docker compose exec web rails db:seed

特に、完了しました!などの出力は返ってきません。失敗した場合はエラー表示されると思います。
成功した場合はデータベースに保存されます。
問題なく保存されているかRailsコンソールで確認します。

myapp(dev)> Profile.all
  Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" /* loading for pp */ LIMIT $1  [["LIMIT", 11]]
=> [#<Profile:0x0000ffff7dc7ba50 id: 3, name: "樹", age: 79, pokemon: "ヒトカゲ", created_at: "2026-02-05 11:36:58.870726000 +0000", updated_at: "2026-02-05 11:36:58.870726000 +0000">]

無事に1レコード作成され、データベースに保存されました。

  • 名前:樹
  • 年齢:79
  • 好きなポケモン:ヒトカゲ

10レコード、いや100レコードまとめて作成したい

Rubyの繰り返し処理 timesメソッドを使用します。
編集場所:db/seeds.rb

10.times do
  Profile.create!(name: Faker::Name.first_name,
                  age: Faker::Number.between(from: 1, to: 100),
                  pokemon: Faker::Games::Pokemon.name
  )
end
[<Profile:0x0000ffff98884148 id: 4, name: "真央", age: 30, pokemon: "ニドキング", created_at: "2026-02-05 11:47:53.650920000 +0000", updated_at: "2026-02-05 11:47:53.650920000 +0000">,
 <Profile:0x0000ffff9887c6c8 id: 5, name: "葵", age: 90, pokemon: "ヒマナッツ", created_at: "2026-02-05 11:47:53.660242000 +0000", updated_at: "2026-02-05 11:47:53.660242000 +0000">,
 <Profile:0x0000ffff9887c588 id: 6, name: "悠太", age: 12, pokemon: "ニャース", created_at: "2026-02-05 11:47:53.666418000 +0000", updated_at: "2026-02-05 11:47:53.666418000 +0000">,
 <Profile:0x0000ffff9887c448 id: 7, name: "智子", age: 29, pokemon: "ハリーセン", created_at: "2026-02-05 11:47:53.678491000 +0000", updated_at: "2026-02-05 11:47:53.678491000 +0000">,
 <Profile:0x0000ffff9887c308 id: 8, name: "綾乃", age: 5, pokemon: "メノクラゲ", created_at: "2026-02-05 11:47:53.684295000 +0000", updated_at: "2026-02-05 11:47:53.684295000 +0000">,
 <Profile:0x0000ffff9887c1c8 id: 9, name: "彩", age: 39, pokemon: "デンリュウ", created_at: "2026-02-05 11:47:53.694848000 +0000", updated_at: "2026-02-05 11:47:53.694848000 +0000">,
 <Profile:0x0000ffff9887c088 id: 10, name: "悠斗", age: 85, pokemon: "クサイハナ", created_at: "2026-02-05 11:47:53.702730000 +0000", updated_at: "2026-02-05 11:47:53.702730000 +0000">,
 <Profile:0x0000ffff9887bf48 id: 11, name: "隼人", age: 92, pokemon: "ランターン", created_at: "2026-02-05 11:47:53.705222000 +0000", updated_at: "2026-02-05 11:47:53.705222000 +0000">,
 <Profile:0x0000ffff9887be08 id: 12, name: "楓", age: 44, pokemon: "ナッシー", created_at: "2026-02-05 11:47:53.708771000 +0000", updated_at: "2026-02-05 11:47:53.708771000 +0000">,
 <Profile:0x0000ffff9887bcc8 id: 13, name: "杏", age: 18, pokemon: "エレキッド", created_at: "2026-02-05 11:47:53.710985000 +0000", updated_at: "2026-02-05 11:47:53.710985000 +0000">]

(※先ほど作成したデータは実行前に削除しました)
10レコード作成し、データベースに保存されました。
100件であれば、100.times do、1000件であれば1000.times doに直して実行してください。

(補足)重複を回避したい場合

Fakerはランダムでデータを生成しますが、同じ名前を生成することがあります。

name: Faker::Name.unique.first_name

このような形で、uniqueを追記してあげると、データのある限りFakerは重複を回避します。
もしダミーデータが底をつきるとエラーになるので注意して下さい。
また、モデル側でバリデーションを設定しているとuniqueを記載しなくても重複を回避することができます。

おわりに

Fakerについて一通り学ぶことができました。
まさか「ポケモン」があるとは思ってなくてびっくりしました。しかも日本語対応!
他にも「ジブリ」や「ナルト」など、日本のアニメ文化がしっかり反映されているので少し見入ってしまいました。
local/jaディレクトリに日本語対応しているFakerが記載されていましたので
ぜひ興味ある方はご覧ください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?