使用 mongoid 参考嵌入文档
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论