[RAILS] Create related data together with FactoryBot for yourself

Use after (: crate)

case

When user updates to teacher_account Create user_performance as related data.

user_performance uses polymorphic related

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

Recommended Posts

Create related data together with FactoryBot for yourself
Create multiple instances together from FactoryBot with RSpec using create_list
Docker related commands (notes for yourself)
Create realistic dummy data with gem Faker
FactoryBot Register multiple data with the same model
Create dummy data of portfolio with Faker [Note]
Create an HTTPS file server for development with ring-jetty-adapter
Create a widget template for iOS14 with Intent Configuration.
Generate dummy data for various tests with Faker (java)
Create an EC site with Rails5 ⑥ ~ seed data input ~
[Rails] Create initial data with seed.rb [Faker] [Japanese localization]
Introduction to RSpec 4. Create test data with Factory Bot