ruby-on-rails-3 – 如何更新工厂女孩的嵌套属性值

我们有一个工厂,我们正在开发嵌套关系

FactoryGirl.define do
  factory : user do
    name "Arvind"
    association :campaign
    factory :user_with_photos do
      after(:build) do |user|
        user.photos.build name: 'user 1', 
        user.photos.build name: 'user 2',  position: 2
      end
    end
    factory :user_with_mandatory_photos do
      after(:build) do |user|
        user.photos.build name: 'user 3',  mandatory: true
        user.photos.build name: 'user 4',  mandatory: true
      end
    end

    factory :user_with_mandatory_photos do
      after(:build) do |user|
        user.photos.build name: 'user 5',  mandatory: false
        user.photos.build name: 'user 6',  mandatory: false
      end
    end
  end
end

我们正在创建属性

 users = FactoryGirl.create(:user_without_mandatory_fields, campaign: @campaign)

 users = FactoryGirl.create(:user_with_mandatory_fields, campaign: @campaign)

我们希望减少这种情况并仅使用它

factory : user do
    name "Arvind"
    association :campaign
    factory :user_with_photos do
      after(:build) do |user|
        user.photos.build name: 'user 1', 
        user.photos.build name: 'user 2',  position: 2
      end
    end
end

但是我们没有得到如何使用此方案中的必填字段创建用户照片.任何人都可以建议吗?

Looking something like  users = FactoryGirl.create(:user_with_photos,[update user photo attribute make mandatory true ] campaign: @campaign)

最佳答案 修复了创建新照片对象的问题

photos = FactoryGirl.create_list(:photo, 12, mandatory: true)
users = FactoryGirl.create(:use, :photos, campaign: @campaign)
点赞