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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:01:13+00:00 2026-05-16T02:01:13+00:00

I have two classes Message and User. Message has sender_id and recipient_id both foreign

  • 0

I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I’ll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name

I tried to do it by this way:

class Message < ActiveRecord::Base  

  belongs_to :sender, :class_name => 'User', :foreign_key => 'sender'
  belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient'

end   

class User < ActiveRecord::Base

  has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient'
  has_many :send_messages, :class_name => 'Message', :foreign_key => 'sender'
end

But it didn’t help, when I’m trying to access to, for instance, @message.recipient.name it says that “undefined method `name'”

  • 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-16T02:01:14+00:00Added an answer on May 16, 2026 at 2:01 am

    You can use the :class_name property to set which class gets used for a foreign key:

    class Message < ActiveRecord::Base
      has_one :sender, :class_name => User
      has_one :recipient, :class_name => User
    end
    
    class User < ActiveRecord::Base
      belongs_to :sent_messages, :class_name => Message
      belongs_to :received_messages, :class_name => Message
    end
    

    Also, you say you are using sender_id and recipient_id for the foreign keys, but in your code you have :foreign_key => 'sender' and :foreign_key => 'recipient'. Have you tried changing them to :foreign_key => 'sender_id' and :foreign_key => 'recipient_id'? So:

    class Message < ActiveRecord::Base
      has_one :sender, :class_name => User, :foreign_key => 'sender_id'
      has_one :recipient, :class_name => User, :foreign_key => 'recipient_id'
    end
    
    class User < ActiveRecord::Base
      belongs_to :sent_messages, :class_name => Message, # ...etc
      belongs_to :received_messages, :class_name => Message, # ...etc
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes. I created a JAR file using: jar cvf practice.jar class1.class
I have two classes in my project that I would like to pass the
I am new to LINQ. I have a class like this: public class StudentResults
Two questions. 1) I understand this must be an expected outcome but maybe someone
All this might look too trivial but read it through - I have simple
I am developing a WPF Application using MVVM Architecture. I am an amateur in
I'm having a problem with an if/else block. The problem is that the else
I am trying to figure out why I can't specify a target as being
We're trying to create a simple to use webservice for logging exceptions and messages.
why (if at all) is this a bad idea ? class Program { static

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.