Get two associations within a Factory to share another association

后端 未结 6 1646
情书的邮戳
情书的邮戳 2021-02-12 14:57

I\'ve got these 5 models: Guardian, Student, Relationship, RelationshipType and School. Between them, I\'ve got these associations

class Guardian < ActiveReco         


        
6条回答
  •  日久生厌
    2021-02-12 15:31

    I think this should work:

    FactoryGirl.define do
      factory :relationship do 
        association :guardian
        relationship_type RelationshipType.first
        after_build do |relationship|
          relationship.student = Factory(:student, :school => relationship.guardian.school)
        end
      end
    end
    

提交回复
热议问题