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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:35:06+00:00 2026-05-25T09:35:06+00:00

I am running a rails 3 app that models email messages. The app is

  • 0

I am running a rails 3 app that models email messages. The app is deployed on on Heroku so the backend db is PostgreSQL. Messages threads are simple modeled by the thread_id field in the Posts table. When the user post a new Post, let’s call it p1 then p1.thread_id=p1.id. If the user reply to p1 with p2 then p2.thread_id=p1.thread_id.

I need to write a query to select the messages sent to a certain user. The resulting list must contain only one message per thread and that message must be the latest message in the thread. I also need to know how many messages are in each thread.

Using the following select:

SELECT DISTINCT ON(thread_id) * FROM "posts"

does not work as it does not returns the post sorted by last first.

This one does not work either:

SELECT DISTINCT ON(thread_id) * FROM “posts” ORDER BY thread_id, posts.created_at DESC

as the posts are ordered first by thread_id.

Posts table:

create_table "posts", :force => true do |t|
    t.string   "title"
    t.text     "content"
    t.string   "content_type"
    t.text     "options"
    t.string   "type"
    t.integer  "receiver_id"
    t.integer  "sender_id"
    t.integer  "circle_id"
    t.text     "data_bag"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "parent_id"
    t.integer  "thread_id"
    t.datetime "posted_at"
  end

Thanks for any help.

  • 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-25T09:35:07+00:00Added an answer on May 25, 2026 at 9:35 am

    If you don’t mind getting your hands dirty with a bit of SQL you can use a window function to get the job done. You can get the post IDs with this SQL:

    select id
    from (
        select id,
               rank() over (partition by thread_id order by created_at desc)
        from posts
        where receiver_id = #{user.id}
    ) as dt
    where rank = 1
    

    If you want more columns add them to both SELECT clauses. The #{user.id} is, of course, the recipient that you’re interested in.

    The interesting part is the window function:

    rank() over (partition by thread_id order by created_at desc)
    

    This will partition the table into groups based on thread_id (sort of a localized GROUP BY), order them by the timestamp (most-recent first), and then rank() yields 1 for the first entry in each group, 2 for the second, etc.

    Given a table that looks like this:

    => select * from posts;
     id | receiver_id | thread_id |     created_at      
    ----+-------------+-----------+---------------------
      1 |           1 |         2 | 2011-01-01 00:00:00
      2 |           1 |         2 | 2011-02-01 00:00:00
      3 |           1 |         2 | 2011-03-01 00:00:00
      4 |           1 |         3 | 2011-01-01 00:00:00
      5 |           1 |         4 | 2011-01-01 00:00:00
      6 |           1 |         3 | 2011-01-01 13:00:00
      7 |           2 |        11 | 2011-06-06 11:23:42
    (7 rows)
    

    The inner query gives you this:

    => select id, rank() over (partition by thread_id order by created_at desc)
       from posts
       where receiver_id = 1;
    
     id | rank 
    ----+------
      3 |    1
      2 |    2
      1 |    3
      6 |    1
      4 |    2
      5 |    1
    (6 rows)
    

    And then we wrap the outer query around that to peel off just the top ranking matches:

    => select id
        from (                                                                  
            select id,
                   rank() over (partition by thread_id order by created_at desc)
            from posts
            where receiver_id = 1
        ) as dt
        where rank = 1;
    
     id 
    ----
      3
      6
      5
    (3 rows)
    

    So add the extra columns you want and wrap it all up in a Post.find_by_sql and you’re done.

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

Sidebar

Related Questions

I've got a Rails app that's been running live for some time, and I'm
I have an app running Rails 2.3.5 that has a JSON API for much
I have a single-threaded Rails app running on thin in single-threaded mode on Heroku
I have RailsAdmin running for my Rails 3 app which has two models -
I have got a Rails app running which displays data that is generated by
I have a simple Rails 3 app: no models, one view and one controller
I've got a Rails app that's running in an iframe in a canvas app
I am running a rails app on Dreamhost. Today, a strange thing happened. A
I have a ruby on rails app running a server and sometimes it needs
I have a Ruby on Rails app running on my server, and I can't

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.