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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:40:51+00:00 2026-05-21T11:40:51+00:00

I am trying to perform a query like the following using ActiveRecord and Rails

  • 0

I am trying to perform a query like the following using ActiveRecord and Rails 3:

select count(*), * from messages group by thread_id

I am trying to build the inbox of a simple threaded message system. The point of this query is to collapse all Message object’s to only the most recent one per thread and to also retrieve the size of the thread.

Alternatively, I could just retrieve all the top Messages per thread (simply by removing the count(*)) and then perform the count as a separate query per group of messages, but this seems horribly inefficient.

Does anyone have any ideas? Is there a way to do this in ActiveRecord?

  • 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-21T11:40:51+00:00Added an answer on May 21, 2026 at 11:40 am

    To get rails (3) to generate the query you want, try:

    Message.select('count(*) as thread_size, *').group(:thread_id)
    

    The number of messages in the thread will then be available as the thread_size attribute of the Message records that are returned.

    Should you find that this query starts to become slow as your database grows larger, you might want to look into Rails’ counter_cache functionality (see Section 4.1.2.4 of the ActiveRecord Rails Guide).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a LINQ query on a DataTable object and bizarrely I
I'm trying to perform a SQL query through a linked SSAS server. The initial
I'm trying to perform a simple LINQ query on the Columns property of a
I'm trying to perform a bitwise NOT in SQL Server. I'd like to do
I'm trying to perform a group by action on an aliased column (example below)
In this situation I am trying to perform a data import from an XML
I am trying to create an object that is capable of handling query-like conditional
I am trying to query this very complicated XML document using xDocument and LINQ
I'm having some problems trying to perform a query. I have two tables, one
Trying to perform a single boolean NOT operation, it appears that under MS SQL

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.