2 models:
Class User
include Mongoid::Document
has_many :reports
end
Class Report
include Mongoid::Document
belongs_to :user
end
I need a query to get all users have 6 or more reports, something like:.
Users.where(reports.count > 5)
I use mongo id 2.4.12
How can I do it?
Thank you very much!
Ok, This query is not possible since MongoDB does not have joins.
The fix for this problem is use a counter cache.
The first option is make a counter cache something like:
The second option (I have used this option) is use this gem https://github.com/jah2488/mongoid-magic-counter-cache.
For this question:
Models:
Inside a helper for example:
Regards!