在 MongoMapper 中获取 EmbeddedDocument 的父文档 ID
假设以下模型。
class Person
include MongoMapper::Document
key :name, String
key :surname, String
many :children
end
class Child
include MongoMapper::EmbeddedDocument
key :name, String
end
另外,以下查询(使用 Sinatra):
get 'child/:id' do
@child = Child.find(params[:id])
end
有没有办法获取该 Child 所属的 Person 的 ID?
Suppose the following model.
class Person
include MongoMapper::Document
key :name, String
key :surname, String
many :children
end
class Child
include MongoMapper::EmbeddedDocument
key :name, String
end
Plus, the following query (with Sinatra):
get 'child/:id' do
@child = Child.find(params[:id])
end
Is there a way to get the ID of the Person that that Child belongs to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找的是这样的:
我不太确定您的查询是如何工作的 - 我没有看到 Child 类上有找到,因为它是一个 EmbeddedDocument。但是:
应该可以。
I think what you're looking for is this:
I'm not quite sure how your query works - I'm not seeing that there's a find on the Child class since it's an EmbeddedDocument. However:
should work.