Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

factory girl - passing association data

I am trying to change attributes which are set when saving associations

Factories:

Factory.define :course do |course|
 course.title "Course 1"
end

Factory.define :user do |user|
 user.name "Alex"
end

Execution

Factory(:course, :user => Factory(:user, name: 'Tim'))

The saved value will be 'Alex' not 'Tim'. Any ideas?

like image 788
Alex Avatar asked Jan 18 '23 00:01

Alex


1 Answers

First you need to add the association to your factory:

Factory.define :course do |course|
    course.title "Course 1"
    course.association :user
end

Then you should do it as a 2-step process:

user = Factory.create :user, :name => "Tim"
course = Factory.create :course, :user => user # or :user_id => user.id

And assuming your model associations and such are set up fine, this will work no problem.

like image 86
MrDanA Avatar answered Jan 29 '23 10:01

MrDanA