我试图用与配料有 has_many 关系的食谱播种我的数据库。成分表有 4 行。但是,我的代码一直遇到错误。这是我的代码。我对 Rails 还很陌生,但还没有找到解决方案。我正在使用带有 Postgres 的 Rails 4。
错误
rake aborted!
Ingredient(#xxxxxxx) expected, got Hash(#xxxxxxx)
Tasks: TOP => db:seed
(See full trace by running task with --trace)
食谱
class Recipe < ActiveRecord::Base
attr_accessible :title, :descrition, :image
has_many :ingredients
accepts_nested_attributes_for :ingredients
end
成分
class Ingredient < ActiveRecord::Base
attr_accessible :measure, :modifier, :item, :note
belongs_to :recipe
end
Seed.rb (这里的示例有 2 种成分,每种成分的每一行都有内容)
Recipe.create(
[{
title: "Recipe title here",
description: "This is the description",
image: "theimage.jpg",
ingredients_attributes: [{measure: "1cup", modifier: "canned", item: "Mushrooms", note: "drained"}, {measure: "1lb", modifier: "sliced", item: "Bacon", note: "previously cooked"},]
}],
without_protection: true)