想象一下,我想为BookSupplier
和Book
表格生成数据。
BookSupplier.create(company: "Pablo Lledó - ProjectManagement", city: "Canadá", country: "Estados Unidos", address: "Victoria, BC, Canadá", telephone: "1", contact: "Limberg Morales", email: "", website: "")
Book.create(title: "Ajax", author: "Zakas, Nicholas C.- McPeak, Jeremy - Fawcett, Joe", edition: "", isbn: "84-415-2077-1", pages: "448", publish_date: "Septiembre 2006", cover: "libro_84-415-2077-1_1195672469.jpg")
如何正确地将该书与该BookSupplier相关联?处理这个问题的正确方法是什么?
答案 0 :(得分:2)
使用中间变量来保留供应商。
如果他们已经在你的模特中有关系,那么:
pablo_lledo = BookSupplier.create(company: "Pablo Lledó - ProjectManagement")
Book.create(supplier: pablo_lledo)
否则:
pablo_lledo = BookSupplier.create(company: "Pablo Lledó - ProjectManagement")
Book.create(supplier_id: pablo_lledo.id)