使用 mongoid 参考嵌入文档

发布于 2025-01-06 11:04:54 字数 477 浏览 3 评论 0原文

class Package
  include Mongoid::Document

  field :slug, type: String
  field :created_at, type: Time

  key :slug

  embeds_many :versions, class_name: 'PackageVersion'
end

我正在尝试找到一种方法来引用 :versions 嵌入集合中的最新 PackageVersion 。用于查询和访问。

如果我可以对嵌入文档进行自引用,这样我就可以像...

embeds_one :latest_version, class_name: 'PackageVersion', via: :versions

到目前为止我唯一的解决方案是只存储 id 并添加一个类方法来查找嵌入文档,那就 太好了不允许我使用latest_version轻松查询。

class Package
  include Mongoid::Document

  field :slug, type: String
  field :created_at, type: Time

  key :slug

  embeds_many :versions, class_name: 'PackageVersion'
end

I'm trying to find a way to reference the latest PackageVersion in the :versions embedded collection. For both querying and accessing.

It would be nice if I could have a self-reference to an embedded document so that I could so something like...

embeds_one :latest_version, class_name: 'PackageVersion', via: :versions

My only solution so far has been to just store the id and add a class method to find the embedded one but that doesn't allow me to easily query using the latest_version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文