工厂机器人错误“私有方法`新'调用User:Class(NoMethodError)”

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

我是工厂机器人的新手,我尝试使用工厂机器人创建一个示例数据,但是我收到了这个错误

如何解决此错误?

功能/支持/ factories.rb:

require 'factory_bot'


FactoryBot.define do
  factory :user do
    email "[email protected]"
    password "asdf123"
    password_confirmation "asdf123"
  end
end
FactoryBot.define do
  factory :post do
    user = FactoryBot.create(:user)
  end
end
ruby-on-rails ruby factory-bot
1个回答
0
投票

要求'factory_bot'

FactoryBot.define do
  factory :user do
    email "[email protected]"
    password "asdf123"
    password_confirmation "asdf123"
  end
end

FactoryBot.define do
  factory :post do
    user
  end
end

FactoryBot文档中所述

Associations

可以在工厂内建立关联。如果工厂名称与关联名称相同,则可以省略工厂名称。

factory :post do
  # ...
  author
end

您还可以指定其他工厂或覆盖属性:

factory :post do
  # ...
  association :author, factory: :user, last_name: "Writely"
end
© www.soinside.com 2019 - 2024. All rights reserved.