rajan sthapit
rajan sthapit

Reputation: 4354

Implementing has many through association in factory girl

I have two models books and authors joined by books_authors model. A book has many authors through books_authors model and an author has many books through books_authors model. Now I need to create factories for books and authors.

I have created a book factory say a book with name "Beginning with FactoryGirl" and two factories for authors like say with name "Author 1" and "Author 2". Now, I want to associate the two author factories with the book factory. How can I implement that in the book.rb factory?

Upvotes: 1

Views: 378

Answers (1)

charliequinn
charliequinn

Reputation: 430

Something like this...

Factory.define :author1 :class => Author do |author|
  author.name "Author1"
  author.books {|books| [books.association(:book)]}
end

Upvotes: 1

Related Questions