如何在FactoryGirl中使用自我加入协会?

时间:2013-04-16 02:40:47

标签: ruby-on-rails factory-bot

我有两个简单的工厂:

factory :parent_category, :class => Category do 
  name "Garden"
  parent_id 0
  display_order 1
 end

factory :category do
  association           :parent_id, factory: :parent_category
  name                  "Tools"
  associated_categories ""
  postfix_to_display    ''
  display_order         1
end

但是,当我尝试使用以下方式生成工厂时:

f = FactoryGirl.create(:category)

我收到以下错误:

NoMethodError: undefined method `to_i' for #<Category:0x007fdf50495c00>

我正在使用Rails 3.2.12和FactoryGirl 4.2。

1 个答案:

答案 0 :(得分:2)

尝试:

factory :category do
  association :parent, factory: :parent_category
  ...
end