Get two associations within a Factory to share another association

后端 未结 6 1647
情书的邮戳
情书的邮戳 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:44

    There is cleaner way to write this association. Answer got from this github issue.

    FactoryGirl.define do
      factory :relationship do 
        association :guardian
        student { build(:student, school: relationship.guardian.school) }
        relationship_type RelationshipType.first
      end
    end
    

提交回复
热议问题