Rails 3 Factory Girl + many for many relationships

There are currently no answers to this question with Factory Girl 4.1 (what can I find) - how do you set up many, many relationships inside the factory?

For example, I have Students and Classrooms , which are in many ways using the connection table, so far I have had the following setting:

factory :classroom do name "Foo Class" ... end factory :student do name "John Doe" ... end factory :student_with_classroom, :parent => :student do after(:build) {|student| student.classrooms << classroom} end 

However, this leads to:

 NameError: undefined local variable or method `classroom' for #<FactoryGirl::SyntaxRunner> 

My attempt was guesses for the most part, as I was unlucky to find any non-obsolete syntax for this.

+6
source share
2 answers

In fact, I managed to find the answer I was looking for, buried under many other answers in this SO: How to create has_and_belongs_to_many associations in Factory girl

 factory :classroom do name "Foo Class" ... end factory :student do name "John Doe" ... end factory :student_with_classroom, :parent => :student do classrooms {[FactoryGirl.create(:classroom)]} end 
+18
source

All Articles