我有两个简单的课程
class Band
include Mongoid::Document
field :name, type:String
has_many :members
end
class Member
include Mongoid::Document
field :name, type: String
belongs_to :band
end
在我为测试目的创建了两个对象之后
Band.create(title: 'New Band')
Band.members.create(name: 'New Member')
我得到了下一个数据库状态:
> db.bands.find()
{ "_id" : ObjectId("..."), "title" : "New Band" }
> db.members.find()
{ "_id" : ObjectId("..."), "name" : "New Member", "band_id" : ObjectId("...") }
当我尝试构建 Band 对象的 json 对象时,我得到没有子对象的数据:
{"_id":"...","title":"New Band"}
但我需要这样的东西:
{"_id":"...","title":"New Band", "members" : {"_id":"...","title":"New Member"}}
如何与孩子一起构建json?