How to use a foreign key with FactoryBot ~ Another solution

Introduction

The other day, I posted a case where the value of a foreign key is used in FactoryBot. When I was investigating after that, there was another method, so I will introduce it. If anything, this one is easier. FactoryBot, solution when played by foreign key validation

Last method

FactoryBot.define do
  factory :user do
    Faker::Config.locale = :ja
    room { FactoryBot.create(:room) } #The point is here
    email { Faker::Internet.free_email }
    nickname { Faker::Name.last_name }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }
  end
end

Method using association

FactoryBot.define do
  factory :user do
    Faker::Config.locale = :ja
    #Removed faker in room
    email { Faker::Internet.free_email }
    nickname { Faker::Name.last_name }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }
    
    association :room #← Addendum

  end
end

Like when writing an association in a model association: model name of the foreign key Just do.

Summary

When using FactoryBot as a foreign key, if you obediently set association: model name of the foreign key, it will automatically associate it with the foreign key and generate test information.

Recommended Posts

How to use a foreign key with FactoryBot ~ Another solution
FactoryBot, a solution when played with a foreign key validation
How to delete data with foreign key
How to rename a model with foreign key constraints in Rails
How to use an array for a TreeMap key
How to use mssql-tools with alpine
How to move another class with a button action of another class.
A memorandum on how to use Eclipse
Add foreign key to column with migrate
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to SSH into Ubuntu from a terminal with public key authentication
How to use built-in h2db with spring boot
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
How to generate a primary key using @GeneratedValue
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to use nginx-ingress-controller with Docker for Mac
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to make a factory with a model with polymorphic association
How to use Oracle JDK 9 EA with Travis CI
How to delete a new_record object built with Rails
How to use Z3 library in Scala with Eclipse
How to manually generate a JWT with Rails Knock
How to launch another command in a Ruby program
[How to insert a video in haml with Rails]
How to use JDD library in Scala with Eclipse
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to write an external reference key in FactoryBot
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
How to create a header or footer once and use it on another page
[ruby] How to assign a value to a hash by referring to the value and key of another hash
How to use a structure with variable length array or bit field in Ruby-FFI
How do you create foreign key values ​​in FactoryBot ~ (crying
How to take a screenshot with the Android Studio emulator
How to request a CSV file as JSON with jMeter
[Java] How to use Map