在 MongoMapper 中获取 EmbeddedDocument 的父文档 ID

发布于 2024-11-16 15:19:33 字数 382 浏览 1 评论 0原文

假设以下模型。

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-11-23 15:19:33

我认为您正在寻找的是这样的:

class Child
  include MongoMapper::EmbeddedDocument

  embedded_in :parent
  key :name, String
end

我不太确定您的查询是如何工作的 - 我没有看到 Child 类上有找到,因为它是一个 EmbeddedDocument。但是:

Person.where("children._id" => params[:id]).first.parent

应该可以。

I think what you're looking for is this:

class Child
  include MongoMapper::EmbeddedDocument

  embedded_in :parent
  key :name, String
end

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:

Person.where("children._id" => params[:id]).first.parent

should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文