How to user factory girl to create associated lists with a has_many with a validation that requires it on create

后端 未结 3 1634
无人及你
无人及你 2021-02-01 20:45

In a Rails application, given three models User, Article and Reviewer with the following relationships and validations:

class User < ActiveRecord::Base
  has_         


        
3条回答
  •  眼角桃花
    2021-02-01 21:42

    factory :article do
      reviewers {|a| [a.association(:reviewer)] }
    end
    

    or

    factory :article do
      before_create do |a|
        FactoryGirl.create(:reviewer, article: a)
      end
    end
    

提交回复
热议问题