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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:41:29+00:00 2026-05-27T08:41:29+00:00

I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>

  • 0

I have the following two models:

class Message < ActiveRecord::Base
  belongs_to :to_user, :class_name => 'User'
  belongs_to :from_user, :class_name => 'User'

  has_ancestry #Using the 'ancestry' gem
end

class User < ActiveRecord::Base
  has_many :messages_received, :class_name => 'Message', :foreign_key => 'to_user_id'
  has_many :messages_sent, :class_name => 'Message', :foreign_key => 'from_user_id'
end

Each user is allowed to have one conversation with another user and all the replies should be threaded from the original message.

In my ‘index’ controller action how do I query both sent messages and received messages? For example, if User1 hits ‘/users/2/messages/’ they should see the whole conversation between user1 and user2 (regardless of who sent the first message). Do I need to add a ‘Thread’ model or is there a way to accomplish this with my current structure?

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-05-27T08:41:30+00:00Added an answer on May 27, 2026 at 8:41 am

    You may be better off restructuring this as a conversation to which you can join people than a series of interconnected messages in a chain. For instance:

    class Conversation < ActiveRecord::Base
      has_many :messages
      has_many :participants
      has_many :users, :through => :participants
    end
    
    class Message < ActiveRecord::Base
      belongs_to :conversation
    end
    
    class Participant < ActiveRecord::Base
      belongs_to :conversation
      belongs_to :user
    end
    
    class User < ActiveRecord::Base
      has_many :conversations
      has_many :participants
    end
    

    When a message is sent to someone, create a conversation for it and invite the appropriate parties by adding them to the users list.

    Threaded messaging could be added here by building a parent relationship into the Message itself or using ancestry, though in practice this tends to be over-kill as simple chronological ordering of replies is usually sufficient for most people.

    To track read/unread status you will need an association table between user and messages directly, and this can be tricky, so avoid it unless you need it.

    Keep in mind that some names are reserved by either Ruby or Rails, and Thread is one of them, so you can’t have a model with that name.

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

Sidebar

Related Questions

I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
I have the following two models class ContactField < ActiveRecord::Base end class Address <
I have the following models: class Aircon < ActiveRecord::Base belongs_to :shop belongs_to :brand belongs_to
I have the following models: class Aircon < ActiveRecord::Base belongs_to :shop belongs_to :brand belongs_to
I have the following two models class Post < ActiveRecord::Base has_many :comments end class
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have the following two simple models for users and user groups: class User
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I am in rails 3.1. I have the following models class Tool < ActiveRecord::Base

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.