0
0

More than 3 years have passed since last update.

FactoryBotで関連データを一緒に作成 自分用

Posted at

after(:crate)を使いましょう

case

userがteacher_accountにアップデートした際、
user_performanceを関連データとして作成。

user_performanceはポリモーフィック関連を利用

code

FactoryBot.define do
  factory :user do
    sequence(:username) { |n| "Testuser#{n}" }
    sequence(:email)    { |n| "tester#{n}@example.com" }
    password            { 'password' }
    phone_number        { "0#{rand(0..9)}0#{rand(1_000_000..99_999_999)}" }
    confirmed_at        { Date.today }

    trait :teacher_account do
      teacher   { true }

      after(:create) do |user|
        create(:user_performance, performancable: user)
      end
    end
  end
end

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