简体   繁体   中英

How to get all embeds_many items in Mongoid

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM