Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:54:25+00:00 2026-05-30T18:54:25+00:00

I’m having problems getting my brain around this. I have four models: Account has

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:54:26+00:00Added an answer on May 30, 2026 at 6:54 pm

    Add this to your Account model:

    has_many :list_items, :through => :lists
    

    Then you can do this:

    a = Account.first
    a.list_items.joins(:category).group("categories.name").count
    # => {"foo"=>1, "bar"=>2}
    

    EDIT

    @categories = Category.select("categories.name AS name, accounts.role AS role, count(list_items.id) AS count")
    .joins(:list_items => {:list => :account})
    .group("categories.name, accounts.role")
    .order("count desc")
    .limit(5)
    
    @categories[0].role
    # => Team Leader
    @categories[0].count
    # => 5
    

    You’ll have to add the category fields to the select, and the query looks something like:

    SELECT categories.name, accounts.role, count(list_items.id) 
    FROM list_items INNER JOIN lists ON lists.id = list_items.list_id 
    INNER JOIN categories ON categories.id = list_items.category_id 
    INNER JOIN accounts ON lists.account_id = accounts.id 
    GROUP BY categories.name, accounts.role;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.