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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:10:18+00:00 2026-06-02T11:10:18+00:00

i have a user model and a message model. my user has_many messages and

  • 0

i have a user model and a message model. my user has_many messages and my message belongs_to user.

on my message model, i have the attributes :content, :user_id, :to_id.

the :user_id acts as the foreign key, and can also be thought of as the ‘sender’ while the :to_id can be thought of as the ‘receiver’.

in my message index template, when im trying to display all the messages, i do something like…

<%= link_to message_item.user.name, message_item.user %>

that line works great when showing my received messages (where im the :to_id and :user_id is showing who sent me the message)

however, when im listing the messages under my ‘sent’ category, doing the line of code above will always show my name (because im :user_id) as opposed to the :to_id, which would make more sense.

ideally i would want something like

<%= message_item.to_id %>

for my sent category. is there a way to get the user object mentioned in my :to_id category?
something like message_item.to_id.name wouldn’t work because message_item.to_id returns back an integer and not the user object. is there a way to get it?

at trying to display and linking to the sender, here is my failed attempt

    <% if current_user == message_item.user %>
    <%= message_item.to_id %>
    <% else %>
        <%= link_to message_item.user.name, message_item.user %>
    <% end %>

thanks

  • 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-02T11:10:20+00:00Added an answer on June 2, 2026 at 11:10 am

    What you need to do is define the relationship for the receiver in message.rb:

    belongs_to :to,
      :class_name => 'User',
      :foreign_key => :to_id
    

    I find it’s usually more clear if you call things like to_id to be to_user_id so it’s clear that you’re talking about a user and not something else.

    Now you can call:

    <%= link_to(message.to.name, message.to) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model User which has_many :messages and Message which belong_to :user. when
I have a User and Group model. User has_many Groups and Group belongs_to User
I have a message model and a user model. my message belongs_to my user
message and user. my message belongs_to user and user has_many messages. in one of
I have models User and Message. Every user can send messages to other users.
I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have a User model, a Post model, and an Interest model. User has_many
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships

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.