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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:27:17+00:00 2026-06-11T17:27:17+00:00

I have a User class with a has_many :messages and a Message class which

  • 0

I have a User class with a has_many :messages and a Message class which belongs_to :user. In the Message controller’s show action has the corresponding view:

<% if @messages.any? %>
  <ol class="microposts">
    <%= render partial: 'shared/message', collection: @messages %>
  </ol>
<% end %>

And the shared/_message.html.erb template looks like this:

<li id="<%= message.id %>">
  <span class="content"><%= message.body %></span>
  <% user_id = message.from %>
  <% user = User.find(user_id) %>
  From: <%= user.name %>
</li>

I feel like the following two lines should be done in the Messages controller from what I read in tutorials on Rails:

<% user_id = message.from %>
<% user = User.find(user_id) %>

But how would I pass each message’s corresponding from value (which stores user.id) into the partial?

thanks,
mike

  • 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-11T17:27:19+00:00Added an answer on June 11, 2026 at 5:27 pm

    So far i have understood that you have a from column in your messages table which stores the user_id. So if you call message.from it returns the user_id. Then you can do the following

    class Message < AR::Base
         belongs_to :user, :foreign_key=>'from' 
    end
    

    In your controller

    @messages = Message.all.includes(:user)
    #It will load messages and will associated users eager loaded.
    

    Now in your view you can call message.user.name to get the user’s name directly. You dont need these following two lines in your view at all


    <% user_id = message.from %>
    <% user = User.find(user_id) %>

    Let me know if ive missed anything.


    UPDATE:

    Here is what Ive tried with

    User Model

    class User < ActiveRecord::Base
        attr_accessible :age, :name
        has_many :sent_messages,    :class_name=>'Message', :foreign_key => 'from'
        has_many :received_messages,:class_name=>'Message', :foreign_key => 'user_id'
    end
    #Contains Data:
    #<User id: 1, name: "p1", age: 27, ... ...>, 
    #<User id: 2, name: "p2", age: 25, ... ...
    

    Message Model

    class Message < ActiveRecord::Base
        attr_accessible :body, :sender, :title, :receipent
        belongs_to :sender,     :class_name => 'User', :foreign_key => 'from'
        belongs_to :receipent,  :class_name => 'User', :foreign_key => 'user_id'
    end
    

    Now say p1 is sending a message to p2

    p1.sent_messages.create(title: "test title", body: "test body", receipent: p2)
    #Result:
    #<Message id: 1, title: "test title", body: "test body", user_id: 2, from: 1 ... ... >
    

    Now for a message, you can get both sender and receipent directly like

    @messages = Message.includes(:sender, :receipent) # You can load only sender or only receipent
    
    #In View
    message.sender.name 
    #OR
    message.receipent.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 models class User < ... belongs_to :language has_many :posts end class
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor
In my application I have a class which has properties of user-defined types like
I have a Blog domain class, which has many messages: class Blog { String
I have a users_manager engine which has a User model class. In an other
I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
I have 2 models User and Address . class User < ActiveRecord::Base has_many :addresses
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have this relationship : class Organization < ActiveRecord::Base has_many :users end class User
I have a class Bar that has a user-defined list of config keys and

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.