2013-03-09 66 views
0

我有一個Note對象附加到Course,我想在FactoryGirl中隨機設置@note.numberrand(@note.course.sections)。我試過:FactoryGirl設置屬性與關聯

factory :note do 
    association :course 
    number { ranb(course.sections) } 
    content { Faker::Lorem.paragraphs(paragraph_count = 1).join(" ") } 
    end 

它不工作,並說課程爲零。什麼是正確的方法來做到這一點?謝謝!

回答

0

我不明白Course#sectionsNote#number之間的關係,我也只能假設你已經定義了Course工廠。我測試了以下,它工作正常:

FactoryGirl.define do 
    factory :course do 
    sequence(:sections) 
    end 

    factory :note do 
    course 
    number { rand(course.sections) } 
    end 
end 


note = FactoryGirl.create(:note) 
# => <Note id: 11, course_id: 12, number: 6, ...> 
note.course 
# => <Course id: 12, sections: 9, ...> 
+0

謝謝,我應該在控制檯也測試過它。錯誤實際上是由'FactoryGirl.attributes_for(:note)'發出的,因爲我沒有通過'course:'。 – randomor 2013-03-09 21:19:06

相關問題