2011-08-11 1 views
1

나는 내 단위에 몇 가지 단위를 더하는 rspec 테스트를 작성했다.공장 아가씨 모임

두 개의 모델 => 루 브릭과 단위가 있습니다. 루 브릭에는 많은 유닛이 있습니다. 은 다음과 같습니다

@rubric.units.push Factory :text_unit 
@rubric.save 

가 그럼 난 factory_girl을 발견하고 공장으로이 코드를 다시 작성했습니다. 하지만 작동하지 않습니다.

어떻게하면이 연관성을 팩토리 걸에 쓸 수 있습니까?

factory :common_rubric , :class => :common_info_rubric do |f| 
    f.sequence(:name) {|n| "common_info_rubric#{n}"} 
    end 

    factory :text_unit, :class => text_info_unit do |f| 
    f.association :common_rubric_with_unit 
    f.sequence(:name) {|n| "text_unit#n}" } 
    end 

    factory :common_rubric_with_unit , :parent => :common_rubric do |f| 
    f.units { |unit| unit.association(:text_info_unit) } 
    end 

난 항상 오류

SystemStackError: 
     stack level too deep 
+0

질문 모든 문제가 모델에서 테이블의 undefault 이름이었다 – nub

+0

업데이트되었습니다. 그리고 읽은 후에 http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl 나는 문제를 해결한다. 그것은 그것 여기에서 http://pastie.org/2355251 (왜냐하면 나는 나의 질문을 위해 대답을 기울인다 7 시간 떠날 때까지) – nub

답변

1

모든 문제는 모델에서 테이블의 undefault 이름이었다. http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl 을 읽은 후 와 나는 문제를 해결

factory :common_rubric , :class => :common_info_rubric do |f| 
    f.sequence(:name) {|n| "common_info_rubric#{n}"} 
    end 

    factory :text_unit, :class => :text_info_unit do |f| 
    f.sequence(:name) {|n| "text_unit#{n}" } 
    end 

    factory :common_rubric_with_unit, :parent => :common_rubric do |f| 
    f.after_create {|a| Factory(:text_unit, :rubric => a) } 
    end 
6

당신이 순환 참조가 있습니다 이 시도. text_unit을 만들면 연결된 common_rubric_with_unit이 생성됩니다. common_rubric_with_unit에 대한 정의는 연결된 text_unit을 만들고 시작합니다.

는 당신은 양쪽에서 협회 중 하나를 제거해야합니다

이 작동합니다 :

factory :text_unit, :class => text_info_unit do |f| 
    f.association :common_rubric_with_unit 
    f.sequence(:name) {|n| "text_unit#n}" } 
end 

factory :common_rubric_with_unit , :parent => :common_rubric do |f| 
end