在Rails中嵌套属性的嵌套工厂女孩

时间:2014-05-23 07:39:50

标签: ruby-on-rails factory-bot

有2家工厂(使用FactoryGirl):

FactoryGirl.define do

  factory :owner do
    name                                'Some name'
    email                               'test@gmail.com'
    phone                               '89020000000'
    password                            '123456'
    password_confirmation '123456'
    role
  end

end

角色:

FactoryGirl.define do

  factory :role do
    description 'Owner'
  end

  factory :superuser_role do
    description 'Superuser'
  end
end

我想添加一个:超级用户工厂,其字段与:除了role之外的所有者。我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:2)

我认为这会起作用

FactoryGirl.define do

  factory :owner do
    name                                'Some name'
    email                               'test@gmail.com'
    phone                               '89020000000'
    password                            '123456'
    password_confirmation '123456'
    role

    factory :superuser do
      association :role, factory: :superuser_role
    end
  end

end