我正在尝试使用 Mongoid使用父引用来创建模型树结构
,
但父级设置为 null。
这是我的课:
class Category
include Mongoid::Document
field :name, type: String
belongs_to :parent, :class_name => 'Category'
end
这就是我创建类别的方式:
parent = Category.new(name: "Mobile").save!
child1 = Category.new(name: "Android", parent: parent).save!
child2 = Category.new(name: "iOS", parent: parent).save!
结果:
{
"categories": [
{
"_id": "511b84c5cff53e03c6000126",
"name": "Mobile",
"parent_id": null,
},
{
"_id": "511b84c5cff53e03c6000128",
"name": "Android",
"parent_id": null,
},
{
"_id": "511b84c5cff53e03c6000129",
"name": "iOS",
"parent_id": null,
}
]
}
父级甚至没有存储在数据库中:
{ "name" : "Mobile", "_id" : "511b84c5cff53e03c6000126" }
{ "name" : "Android", "_id" : "511b84c5cff53e03c6000128" }
{ "name" : "iOS", "_id" : "511b84c5cff53e03c6000129" }
做错了什么?
谢谢!
荣