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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:39:15+00:00 2026-06-14T11:39:15+00:00

Hi I’m trying to understand how relationships with certain conditions work. I’m trying to

  • 0

Hi I’m trying to understand how relationships with certain conditions work. I’m trying to make Messages belong to Users, with my Messages model linked with 2 users (a receiver and a sender). At the same time, Users have 2 different messages (sent + received).

From my research, it seems like this is the way to go:

Users model

class Users < ActiveRecord::Base
  attr_accessible :age, :gender, :name

  has_many :sent_messages, :class => "Messages", :foreign_key => 'sender_id'
  has_many :received_messages, :class => "Messages", :foreign_key => 'receiver_id'
end

Messages model

class Messages < ActiveRecord::Base
  attr_accessible :content, :read

  belongs_to :sender, :class => "User", :foreign_key => 'sender_id'
  belongs_to :receiver, :class => "User", :foreign_key => 'receiver_id'
end

However, I’m having time conceptualizing how I would specify what type of User (a sender or receiver) and what type of Message (received or sent) in the form.

<%= form_for(@user, @message) do |f| %>
    <%= f.label :content %>
    <%= f.text_area :content %>
    <%= f.submit %>
<% end %>

(Let’s say I have authentication) Where/How would I specify that this form’s @user should have this message added to his/her @user.received_messages, while current_user(whoever is logged in) has this message be added to current_user.sent_messages ? Would this be in the message controller under the create action? I’m not sure how I would put values of @user.id = sender_id and current_user.id = receiver_id (or whether I need to do that at all). 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-14T11:39:16+00:00Added an answer on June 14, 2026 at 11:39 am

    All you need to do is to create the message record with the proper user ids attached. The relation will take care of ensuring that the message is included in each respective user’s list of messages (sent and received).

    The current_user you might attach in the controller, as you know this id from the session and don’t need (or want) it in the form.

    The receiver you could just include in the form via a hidden id (or a dropdown, etc, if you need to select the user in the form). If you used a hidden id it’s assumed you’re setting the receiver on the message before rendering the form.

    Something like:

    <%= form_for(@message) do |f| %>
      <%= f.hidden_field, :receiver_id %>
      <%= f.label :content %>
      <%= f.text_area :content %>
      <%= f.submit %>
    <% end %>
    

    And in the controller, something like:

    def create
      @message = Message.new(params[:message])
    
      # If receiver_id wasn't attr_accessible you'd have to set it manually.
      # 
      # This makes sense if there are security concerns or rules as to who 
      # can send to who.  E.g. maybe users can only send to people on their
      # friends list, and you need to check that before setting the receiver.
      #
      # Otherwise, there's probably little reason to keep the receiver_id
      # attr_protected.
      @message.receiver_id = params[:message][:receiver_id]
    
      # The current_user (sender) is added from the session, not the form.
      @message.sender_id = current_user.id
    
      # save the message, and so on
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.