我有一个表单,它有一个类别模型和嵌入的文档,称为“FieldModule”,它嵌入了名为“SubFieldModule”的文档
例如
class Category
include MongoMapper::Document
key :name, String
many :field_modules
end
class FieldModule
include MongoMapper::EmbeddedDocument
key :name, String
many :sub_field_modules
end
class SubFieldModule
include MongoMapper::EmbeddedDocument
key :name, String
end
在我的控制器中,我编辑了我的动作:
@category = Category.find(params[:id])
3.times do
@category.field_modules << FieldModule.new()
end
为该类别设置 3 个 FieldModules。
我希望能够像这样对每个 FieldModules SubFieldModules 做同样的事情
@category.field_modules.each do |mf|
mf << SubFieldModule.new()
end
但它不起作用。
我得到错误:
NoMethodError in Sub categoriesController#edit
undefined method `<<' for #<FieldModule name: nil, _id: $oid4c2b9f594248ce19f000011b>
有人帮我解决这个问题吗?因为我需要更深入地做同样的事情。