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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:13:38+00:00 2026-06-01T13:13:38+00:00

i have a User model and a UserMessage model (a model for holding the

  • 0

i have a User model and a UserMessage model (a model for holding the private messages between two users)

in my view i have..

<% if @message_items.any? %>
    <ol class="messages">
        <%= render partial: 'message_item', collection: @message_items%>
    </ol>
    <%= will_paginate @message_items %>
<% end %>

which i render with…

<li id="<%= message_item.id %>">
    <span class="user">
        <%= link_to message_item.user.name, message_item.user %>
    </span>
    <span>
        <%= message_item.title %>
    </span>
    <span>
        <%= message_item.body %>
    </span>
</li>

how is the object UserMessage(which is coming from message_item) able to render the User object? my design for the UserMessage just has the following attributes “id, user_id, from_id, title, body, created_at, updated_at”.

i guess its from the user_id, and rails somehow makes the connection and is able to find the User object from the user_id. is that correct?

but i what i really want though, is the user from the from_id (the person sending the message). is there a way to retrieve that? i know doing something like.. message_item.user.from_id does not work.

the only way i could think of that works is by doing

<%= User.find(id= message_item.from_id).name %> 

but that doesn’t seem right putting so much code in my view. sorry but ive been super stuck. help would be much appreciated. 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-01T13:13:39+00:00Added an answer on June 1, 2026 at 1:13 pm

    i think you are looking for foreign_key option for belongs_to in model. So what you need is to specify something like sender/from relation with from_id in UserMessage message

    belongs_to :sender, foreign_key: :from_id, class_name: "User"
    

    Then in template you just call this relation in view.

    message_item.sender
    

    It should work same as message_item.user.

    For further reference visit documentation for associations

    In addition i recommend you not call .name method in template, but specify to_s method in your model. Good approach pointed by klump in his answer is to use .include method for better performance. It will load user data while loading UserMessage data, not in another query.

    Article.find :all, :include => :revisions
    

    Code was derived from another answer — Rails ActiveRecord: Is a combined :include and :conditions query possible?

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

Sidebar

Related Questions

I am a user model and in my view I have: @user = current_user
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have a User model that Devise manages - i.e. all users have email
I have a User model and a Friendship-Model. class Friendship < ActiveRecord::Base belongs_to :sender,
I have a user model with two [NotMapped] string properties Password and ConfirmPassword. These
I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
I have a user model: class User < ActiveRecord::Base end This model is used
I have a user model with a settings page at /users/1/edit Currently you can
I have a user model on which I check to make sure that the
I have a user model that requires the user to change their password every

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.