Factory Girl - Transferring Association Data

I am trying to change the attributes that are set when persisting 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 stored value will be "Alex", not "Tim". Any ideas?

+3


source to share


1 answer


First you need to add an association to your factory:

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

      

Then you must do it as a two step process:



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

      

And if your model associations, etc. set up fine, it won't work.

+8


source







All Articles