为什么我在工厂中收到此错误:必须存在卡?

问题描述 投票:0回答:0

我只是想创建一个“帖子”,但我收到了这个错误:

Failure/Error: let!(:post) { FactoryBot.create(:post, company_id: company.id, card_id: card.id) } 
ActiveRecord::RecordInvalid:
        Validation failed: must be exist card

这就是我创建对象的方式

post

RSpec.describe 'Sections', type: :request do
let!(:company) { FactoryBot.create :company }
let!(:card) { FactoryBot.create(:card, company_id: company.id) }
let!(:post) { FactoryBot.create(:post, company_id: company.id, card_id: card.id) } 

帖子的模型是:

class Post < ApplicationRecord
  validates :first_title, :sub_title, :email, presence: true
  multi_tenant :tenant
  include Discard::Model
  belongs_to :card
  delegate :max, to: :card
end

和卡片:

class Card < ApplicationRecord
  include Discard::Model
end

工厂:

factory :post do
    first_title { Faker::Name.name }
    sub_title { Faker::Name.name }
    email { Faker::Internet.email}
    card_id {}
  end

卡工厂:

factory :card do
  company_id {}
end
ruby-on-rails activerecord factory-bot
© www.soinside.com 2019 - 2024. All rights reserved.