2013-03-09 67 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

相关问题