添加(has_many)与FactoryGirl.create的关联?

时间:2014-02-26 06:16:09

标签: ruby-on-rails factory-bot

我知道在定义Factory时如何创建has_many关联:

factory :user do
    name "John Doe"
    factory :user_with_posts do
      ignore do
        posts_count 5
      end

      after(:create) do |user, evaluator|
        create_list(:post, evaluator.posts_count, user: user)
      end
    end
  end

但是当我实际创建工厂时,我该怎么做呢,例如:

Factory.create(:user, :posts << ??)

1 个答案:

答案 0 :(得分:1)

一种方法是使用这样的块:

FactoryGirl.create(:user) do |user|
  FactoryGirl.create_list(:post, 10, user: user)
end