简体   繁体   中英

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?

Something like this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM