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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:55:32+00:00 2026-05-25T10:55:32+00:00

I am building a simple messaging system with Rails3 that allows users to send

  • 0

I am building a simple messaging system with Rails3 that allows users to send private messages to each other. Each set of users can have a single stream of messages between each other (like texting)

I am confused however on the logic that is used to build the view that shows all current conversations going on for a user. This includes conversations that might only include sent messages from myself.

My schema has the following:

  create_table "messages", :force => true do |t|
    t.integer  "from_id"
    t.integer  "to_id"
    t.string   "message"
    t.datetime "created_at"
    t.datetime "updated_at"
  end 

I would like to emulate something similar to FB messaging that shows a row for each conversation.`

Any ideas would be super helpful.

Thanks!
Danny

  • 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-25T10:55:33+00:00Added an answer on May 25, 2026 at 10:55 am

    There are two sets of messages to consider:

    • Those where from_id is the current user.
    • Those where to_id is the current user.

    I’d go with a find_by_sql and let the database do all the work:

    chatting_with = User.find_by_sql(%Q{
        select * from users where id in (
            select to_id   as user_id from messages where from_id = :the_user
            union all
            select from_id as user_id from messages where to_id   = :the_user
        )
    }, :the_user => the_user_in_question.id)
    

    An SQL UNION simply does a set-wise union of the two result sets so the above will grab all the users that the_user_in_question has sent messages to and combine that with the users that have sent messages to the_user_in_question; the result will be all the users that are involved in conversations with the_user_in_question as an array of User instances. Since there is an IN on the UNION you can use UNION ALL to avoid a little bit of extra work in the UNION.

    You’d probably want to wrap that in a class method on Message:

    def self.conversations_involving(user)
        User.find_by_sql(%Q{
            select * from users where id in (
                select to_id   as user_id from messages where from_id = :the_user
                union all
                select from_id as user_id from messages where to_id   = :the_user
            )
        }, :the_user => user.id)
    end
    

    And then you could just say things like:

    @other_users = Message.conversations_involving(current_user)
    

    in your controller.

    You’ll want to add indexes to messages.from_id and messages.to_id in your database too.

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

Sidebar

Related Questions

I'm building simple game and I need to set game speed for each level.
I'm building a simple order system and want to send an email after the
I am building a simple Django app that will use scribd to display documents.
I'm building a simple web based forum application. I want to allow users to
I building a simple Framework for OpenGL UI's for MonoTouch. I set up everything
I'm building a simple solid modeling application. Users need to be able to manipulate
I am building a simple e-mail application to send emails to a list of
I building a simple class that is supposed to mimic the functionality of the
I am building a simple site with extJS. I can successfully attach click events
I'm building a simple interpreter of a language that i'm developing, but how i

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.