[Rails]FactoryBotを使ってseedファイルを作る

学習記録

はじめに

実はseedファイルはFactoryBotを使って作成することもできます。Rspecなどのテストファイルだけに使えるものではないんですね。

FactoryBotの作成

まずはFactoryBotを作成します。

FactoryBot.define do
  factory :user do
    sequence(:name) { |n| "MyString#{n}" }
    sequence(:email) { |n| "user+#{n}@example.com" }
    crypted_password { "MyString" }
    salt { "MyStrring" }
  end
end
FactoryBot.defune do
  factory :article do
    user
    sequence(:title) { |n| "MyString#{n}" }
    sequence(:contents) { |n| "MyText#{n}" }
    status { Article.statuses.keys.first }
  end
end

sequenceについて

シーケンスを使うことで、一意の値を生成できます。sequenceブロック内で定義して呼び出されます。

# Defines a new sequence
FactoryBot.define do
  sequence :email do |n|
    "person#{n}@example.com"
  end
end

generate :email
# => "person1@example.com"

generate :email
# => "person2@example.com"

seedファイル

seedファイルを作成します。

10.times.each do
  user = FactoryBot.create(:user)
  article = FactoryBot.create(:araticle, user: user)
  FactoryBot.create(:comment, user: user, article: article)
end

最後にターミナルでコマンドを実行しましょう。

> rails db:seed

データが作成されたか確認します。

> rails c
Running via Spring preloader in process 56407
Loading development environment (Rails 6.0.2.1)
irb: warn: can't alias context from irb_context.

irb(main):001:0> Article.all
   (0.5ms)  SELECT sqlite_version(*)
  Article Load (0.9ms)  SELECT "articles".* FROM "articles" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Article id: 1, user_id: 1, title: "MyString_1", contents: "MyText_1", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 2, user_id: 2, title: "MyString_2", contents: "MyText_2", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 3, user_id: 3, title: "MyString_3", contents: "MyText_3", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 4, user_id: 4, title: "MyString_4", contents: "MyText_4", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 5, user_id: 5, title: "MyString_5", contents: "MyText_5", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 6, user_id: 6, title: "MyString_6", contents: "MyText_6", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 7, user_id: 7, title: "MyString_7", contents: "MyText_7", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 8, user_id: 8, title: "MyString_8", contents: "MyText_8", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 9, user_id: 9, title: "MyString_9", contents: "MyText_9", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<Article id: 10, user_id: 10, title: "MyString_10", contents: "MyText_10", status: "draft", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">]>

irb(main):002:0> User.all
  User Load (0.4ms)  SELECT "users".* FROM "users" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 1, name: "MyString_1", email: "user_1@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 2, name: "MyString_2", email: "user_2@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 3, name: "MyString_3", email: "user_3@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 4, name: "MyString_4", email: "user_4@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 5, name: "MyString_5", email: "user_5@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 6, name: "MyString_6", email: "user_6@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 7, name: "MyString_7", email: "user_7@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "2022-07-14 08:40:56", updated_at: "2022-07-14 08:40:56">, #<User id: 8, name: "MyString_8", email: "user_8@example.com", crypted_password: [FILTERED], salt: "MyString", created_at: "202

しっかりデータが作成されていますね。

参考

https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#sequences

コメント

タイトルとURLをコピーしました