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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:46:34+00:00 2026-06-10T07:46:34+00:00

I need to do a union between the messages I sent and the messages

  • 0

I need to do a union between the messages I sent and the messages I received from someone.

Here is the first thing I tried to do in my controller, but the messages are not ordered. I think I need to do a real union?

def listmessages
   @messages1 = current_user.messages.where(:sender_id => params[:sender_id])
   @messages2 = current_user.sent_messages.where(:recipient_id => params[:sender_id])
   @messages = @messages1 + @messages2
end

Here is my User model:

class User < ActiveRecord::Base
   before_create :distribute_points

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

   has_many :sent_messages, foreign_key: "sender_id", class_name:"Message",
       order: "created_at DESC"

   has_many :messages, foreign_key: "recipient_id", order: "created_at DESC"
end
  • 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-10T07:46:35+00:00Added an answer on June 10, 2026 at 7:46 am

    When you use + on results, your messages first loaded into arrays in memory and then these arrays are concatenated. So to save ordering you should sort it again. Ugly… And very slow when history becomes big.

    You may use real UNION but it requires a lot of raw sql (through Message.find_by_sql("your union query")), which is very ugly and hard to maintain.

    In your case it may be better to use OR instead UNION:

    @messages = Message.where('(sender_id = ? AND recipient_id = ?) OR (recipient_id = ? AND sender_id = ?)', current_user.id, params[:sender_id], current_user.id, params[:sender_id])
    

    And there is another (and in my opinion, slightly more ellegant) solution using IN operation (which will work only if your users can’t send messages to themself):

    @messages = Message.where(:sender_id => [current_user.id, params[:sender_id]], :recipient_id => [current_user.id, params[:sender_id])
    

    In both solutions you may use ordering or pagination as usually.

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

Sidebar

Related Questions

I need UNION two tables with creating new field, where 1 for first table,
I have a union opertaion between two tables SELECT ID_1, name_1, surname_1, FROM T_ONE
I need a way to combine multiple EllipseGeometry to make a union between them,
I need to merge two query results as in union, but I want to
I have tried but i am not able to figure this out. I have
I am not asking difference between them, my question is when we need to
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I have to write a query that performs a union between two tables with
which is the fastest way to find the union and intersection between two lists?
I need to merge two tables: Both have a primary key-column date, but with

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.