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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:19:20+00:00 2026-05-23T12:19:20+00:00

Message has_many user_messages. It has two. 1 UserMessage will have the sender as user_id

  • 0

Message has_many user_messages. It has two.

1 UserMessage will have the sender as user_id

The other UserMessage will have the receiver as user_id

Knowing one user, how can I, for any message, find the other?

Here’s what I tried as a method to the Message class, but it fails:

 32   def other_user(one_user)      
 33     um = self.user_messages
 34     um.each do |user_message|
 35       
 36       output_user = User.find(user_message.user_id) unless user_message.user_id == one_user.id
 37     end
 38     
 39     return output_user
 40   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-05-23T12:19:20+00:00Added an answer on May 23, 2026 at 12:19 pm

    This could be implemented as an association enhancement. This will be efficient for high-volume of messages as it pushes processing to DB.

    class User < ActiveRecord::Base
    
        has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id"
        has_many :receivers, :through => :sent_messages, :source => :receiver do 
            def for_message(message_id)
                where("messages.id = ?", message_id)
            end
        end
    
        # Another association mapping for received_messages
    
    end
    
    
    class Message < ActiveRecord::Base
    
        belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
        belongs_to :receiver, :class_name => "User", :foreign_key => "receiver_id"
    
    end
    
    
    #Usage
    User.first.sent_messages
    
    #all users received messages from first user
    User.first.receivers
    
    #all users received messages from first user for message of message_id
    User.first.receivers.for_message(message_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes Message and User. Message has sender_id and recipient_id both foreign
I have two models generated with generate scaffolding, one is a LogBook the other
I have a two models, Group and User User belongs_to Group and Group has_many
I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
My user model has three relations for the same message model, and is using
I have a JSP which has jQuery and wanted to change the action message
I have two tables pertaining to this question: conversations has many messages . The
I have two tables users registered_members I want to confirm values from user table
Hey, need a little help here. I have two models: class User < ActiveRecord::Base
I have a User class with reference to a Message class. The message class

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.