如何将嵌入文档放入嵌入文档中?
我有一个表单,它有一个类别模型和一个名为“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 个 FieldModule。
我希望能够像这样对每个 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>
有人帮我解决这个问题吗?因为我需要更深入地做同样的事情。
I have a form that has a category model and and embeded docuement called "FieldModule" and this has embedded document called "SubFieldModule"
For example
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
In my controller i edit action i have :
@category = Category.find(params[:id])
3.times do
@category.field_modules << FieldModule.new()
end
To set up 3 FieldModules for the category.
I want to be able to do the same for each FieldModules SubFieldModules like so
@category.field_modules.each do |mf|
mf << SubFieldModule.new()
end
but it doesnt work.
i get error:
NoMethodError in Sub categoriesController#edit
undefined method `<<' for #<FieldModule name: nil, _id: $oid4c2b9f594248ce19f000011b>
Anyone help me out on this ? as i then need to take it one level deeper doing the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: