0

想象一下,我想为 aBookSupplierBook表播种数据。

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")

我怎样才能正确地将那本书与那本书供应商相关联?处理这个问题的正确方法是什么?

4

1 回答 1

3

用一个中间变量来保存供应商。

如果他们已经在您的模型中有关系,那么:

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)
于 2013-02-26T18:50:49.103 回答