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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:33:29+00:00 2026-05-20T12:33:29+00:00

In a Rails app, I want to allow users to send messages from one

  • 0

In a Rails app, I want to allow users to send messages from one to another. So, I have a User model that will have this:

has_many :messages

I was thinking that the Message model will have a from field, containing the id of the user that sent it, and a to field, containing the user id of the user that it’s addressed to. What would be the best practice for the Messsage model? Should I generate it like this:

rails g model Message from:integer to:integer title:string content:text

How would I associate it to a user? Should I associate it to 2 users, since the from and to fields reference existing users? How would I represent this relationship? What kind of relationship would I write in the message model? belongs_to :user ?

I feel there should be a way of letting Rails manage the user id’s for me, so that I don’t have to create integer fields myself.

  • 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-20T12:33:29+00:00Added an answer on May 20, 2026 at 12:33 pm
    User
    
    has_many :sent_messages, :class_name => "Message", :foreign_key => 'from'
    has_many :received_messages, :class_name => "Message", :foreign_key => 'to'
    
    Message
    
    belongs_to :sender, :class_name => 'User', :foreign_key => 'from'
    belongs_to :receiver, :class_name => 'User' :foreign_key => 'to'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to integrate chargify to my rails app. I have user object and
I have a Rails app (running 3.2.2) and I want to cache my users'
I have a rails app that I'm going to host on engineyard and want
I have a single Rails 2.2.2 app that I want to 'share' with multiple
I have a rails 3 app that allows users to share files by uploading
I have a rails 3.1 application where I want to allow users to share
I'm writing a rails app that has a model called 'User' and a model
I have a rails app that allows a user to download a generated CSV
I am looking to knock off a quickie rails app that will allow two
In my Rails app I want users to be able to input video links

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.