0

我有一个Note附加到 a 的对象Course,我想在 FactoryGirl中随机设置@note.number为。rand(@note.course.sections)我试过了:

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

它不起作用,并说课程为零。这样做的正确方法是什么?谢谢!

4

1 回答 1

1

I don't understand the relationship between Course#sections and Note#number, also I can only assume you've defined the Course factory. I've tested the following, and it works fine:

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, ...>
于 2013-03-09T20:52:51.530 回答