Reputation: 5663
I have a Note
object attached to a Course
, I want to randomly set the @note.number
to rand(@note.course.sections)
in FactoryGirl. I tried:
factory :note do
association :course
number { ranb(course.sections) }
content { Faker::Lorem.paragraphs(paragraph_count = 1).join(" ") }
end
It doesn't work and says the course is nil. What's the right way to do this? Thanks!
Upvotes: 0
Views: 654
Reputation: 1251
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, ...>
Upvotes: 1