Please forgive me as reading this article does not lead to any solution. It is an output of personal worries.
I'm using Facker to build user test information and test if it can be saved properly.
factories/users.rb
FactoryBot.define do
  factory :user do
    Faker::Config.locale = :ja
    class_room_id { 1 }
    email { Faker::Internet.free_email }
    first_name { Faker::Name.first_name }
    last_name { Faker::Name.last_name }
    nickname { Faker::Name.last_name }
    attendance_number { Faker::Number.between(from: 1, to: 40) }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }
  end
end
user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
  describe 'user registration' do
    before do
      @school = FactoryBot.create(:school)
      @class_room = FactoryBot.create(:class_room)
      @user = FactoryBot.build(:user)
    end
    it 'If all items are entered, you can register' do
       expect(@user).to be_valid
    end
  end
end
"If all items are entered, you can register." This cannot be saved! !! !! !!
--Hypothesis 1 Is it useless even if I associate it with a foreign key (class_room_id)?
--Hypothesis 2 Is it not good to enter the value directly in number_field (class_room_id) with the helper method?
I haven't consulted with you, so I think it's one of the few codes, but if anyone knows the cause of the error, please teach me.
Recommended Posts