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 9034071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:23:36+00:00 2026-06-16T08:23:36+00:00

I have a Model called Person and Person has multiple posts. When I want

  • 0

I have a Model called Person and Person has multiple posts. When I want to query post count for each person it takes a long time to process since it needs to iterate over each person and query each posts to get the aggregation.

class Person < ActiveRecord::Base
has_many :posts
end

Output (JSON):

Person1
  PostsType1Count: 15
  PostsType2Count: 45
Person2
  PostsType3Count: 33
.
.
.

I want to calculate all the post count for each Person in a optimum way. What would be the best solution?

  • 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-06-16T08:23:37+00:00Added an answer on June 16, 2026 at 8:23 am

    Here’s one way to do this, if you have a small and pre-defined set of Types

    class Person < ActiveRecord::Base
      has_many :type_1_posts, :class_name => 'Post', :conditions => 'post_type = 1'
      has_many :type_2_posts, :class_name => 'Post', :conditions => 'post_type = 2'
      has_many :type_3_posts, :class_name => 'Post', :conditions => 'post_type = 3'
    end
    

    Then you can write code that looks like this to get all the data:

    @all_people = Person.includes(:type_1_posts, :type_2_posts, :type_3_posts).all
    

    The eager loading of the posts allows the count of each type of post to be available, as well as all the posts of each type.

    If you need extra performance for this code, because you perform this query a lot, then you can look into using the Rails counter cache mechanism to keep track of the counts of each type on the Person object.

    The beauty of Rails here is that your main display code doesn’t need to change during this process of making the code faster for reading (adding a counter cache makes adding/deleting posts slower, so you may not want it in all cases).

    1. Write initial code
    2. Use eager loading to make it faster
    3. Use counter cache to make it even faster
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a model object 'Person' defined, which has a field called 'Name'.
I have a model called Person. I want User to inherit from Person. class
For instance, I have a model called Person, and it has a bool field
I have a model called Kase each Case is assigned to a contact person
I have created a Django model called Person, which has got a 'user' ForeignKey
I have a Rails model called Person which has database table columns for first_name
I have a model called Event and another called Product. An event has many
I have a model called Business , and I want to save Business.all into
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called a voip_phone that has a an attribute schedule which

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.