I’m having problems getting my brain around this.
I have four models: Account has many-> List has many-> ListItem <-belongs to Category
I need to get a list of the top-n Categories and the quantity count — the ones that have the most related ListItems. To make it more complex, the Account has a “role” (say, ‘team lead’ and ‘manager’, and the count needs to be separated by that role. To summarize, I need this:
Top 5 Categories for Team Lead: Category122 (74), Category342 (67), Category22 (52), Category992 (50), Category12 (47)
Top 5 Categories for Manager: Category1 (174), Category32 (112), Category22 (88), Category92 (73), Category5 (72)
I keep trying to form scopes or write class methods to generate these numbers and don’t get far before I get confused.
Anyone know how this type of thing can be calculated?
Add this to your
Accountmodel:Then you can do this:
EDIT
You’ll have to add the category fields to the select, and the query looks something like: