首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >factory_bot / factory_girl添加belongs_to的语法

factory_bot / factory_girl添加belongs_to的语法
EN

Stack Overflow用户
提问于 2018-07-11 23:04:14
回答 1查看 58关注 0票数 0

我有以下模式:

代码语言:javascript
复制
class SecondaryIabCategory < ApplicationRecord
  has_many :sites, foreign_key: :secondary_category_id
  belongs_to :primary_iab_category
end

以下是有问题的工厂的评论:

代码语言:javascript
复制
  factory :primary_category, class: PrimaryIabCategory do
    name 'Arts & Entertainment - yaddah'
    value 'IAB1-some'
  end

  factory :another_primary_category, class: PrimaryIabCategory do
    name 'Games are cool! - dabbah'
    value 'IAB1-other'
  end

  factory :secondary_category, class:  SecondaryIabCategory do
    # here is the problem - what's the correct syntax?
    primary_iab_category another_primary_category
    name 'Test Secondary Cat'
    value 'xxyy'
  end

我得到了错误信息:

代码语言:javascript
复制
NoMethodError: undefined method `another_primary_category=' for #<SecondaryIabCategory:0x00007f925c64e0d8>

我已经有几个月没有使用FactoryBot/Girl了,所以我不确定它的语法是什么。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 23:27:10

尝尝这个

代码语言:javascript
复制
factory :secondary_category, class:  SecondaryIabCategory do
  association :primary_iab_category, factory: :another_primary_category
  name 'Test Secondary Cat'
  value 'xxyy'
end

鲁比多是非常详细的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51295286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档