Ruby与MongoDB的数据映射及模型构建
一、Ruby数据映射器与MongoDB的结合
1.1 多态嵌入对象示例
在Ruby中使用MongoDB时,我们可以看到 Insurance 对象具有多态性。例如以下代码:
irb> p = Pilot.first
=> #<Pilot _id: 4ef9a410fed0eb977d000002, _type: "Pilot", name: "Gautam", age: nil, address: {"street"=>"asfds", "city"=>"Pune", "_id"=>BSON::ObjectId('4f0491bcfed0ebcc59000001')}, weight: nil, gforce: nil>
irb> p.insurances << TravelInsurance.new
=> [#<TravelInsurance _id: 4f06ad2efed0ebe598000002, _type: "TravelInsurance">]
irb> a = Astronaut.first
=> #<Astronaut _id: 4f069fd8fed0ebe45d000001, _type: "Astronaut", name: nil, age: nil, address: nil, weight: nil, gforce: nil>
irb> a.insurances << TravelIns
超级会员免费看
订阅专栏 解锁全文
57

被折叠的 条评论
为什么被折叠?



