如何获取杂种中的所有embeds_many项目

发布于 2025-01-27 01:53:41 字数 400 浏览 1 评论 0原文

我正在使用牛角,我有两个模型:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :tickets

  field :email, type: String

end

class Ticket
  include Mongoid::Document
  include Mongoid::Timestamps

  embedded_in :user

  field :body, type: String

end

如何从ticket为任何用户获取所有记录? 当我使用ticket.all时,我得到nil

I'm Using Mongoid and I have two models:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :tickets

  field :email, type: String

end

class Ticket
  include Mongoid::Document
  include Mongoid::Timestamps

  embedded_in :user

  field :body, type: String

end

How can I do get all records from Ticket for any users?
When I use Ticket.all, I get nil.

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

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

发布评论

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

评论(1

浮生未歇 2025-02-03 01:53:41

不好,但是工作的答案

User.all.map { |user| user.tickets }.flatten

正在工作,但是它会向您提出n请求数据库(n =用户数),不知道是否还有另一种使用mongoid进行此操作的方法。

Bad but working answer

User.all.map { |user| user.tickets }.flatten

It's working but it gonna make N request to you database (N=number of user), don't know if there is another way of doing this with mongoid.

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