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

  • Home
  • SEARCH
  • 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 770475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:30:23+00:00 2026-05-14T18:30:23+00:00

I am putting together a messaging system for a rails app I am working

  • 0

I am putting together a messaging system for a rails app I am working on.
I am building it in a similar fashion to facebook’s system, so messages are grouped into threads, etc.

My related models are:

  • MsgThread – main container of a thread
  • Message – each message/reply in thread
  • Recipience – ties to user to define which users should subscribe to this thread
  • Read – determines whether or not a user has read a specific message

My relationships look like

class User < ActiveRecord::Base
    #stuff...
    has_many :msg_threads, :foreign_key => 'originator_id' #threads the user has started
    has_many :recipiences
    has_many :subscribed_threads, :through => :recipiences, :source => :msg_thread #threads the user is subscribed to
end

class MsgThread < ActiveRecord::Base
    has_many :messages
    has_many :recipiences
    belongs_to :originator, :class_name => "User", :foreign_key => "originator_id"
end

class Recipience < ActiveRecord::Base
    belongs_to :user
    belongs_to :msg_thread
end

class Message < ActiveRecord::Base
    belongs_to :msg_thread
    belongs_to :author, :class_name => "User", :foreign_key => "author_id"
end

class Read < ActiveRecord::Base
   belongs_to :user
   belongs_to :message
end

I’d like to create a new selector in the user sort of like:

has_many :updated_threads, :through => :recipiencies, :source => :msg_thread, :conditions => {THREAD CONTAINS MESSAGES WHICH ARE UNREAD (have no 'read' models tying a user to a message)}

I was thinking of either writing a long condition with multiple joins, or possibly writing giving the model an updated_threads method to return this, but I’d like to see if there is an easier way first. Am I able to pass some kind of nested hash into the conditions instead of a string?

Any ideas? Also, if there is something fundamentally wrong with my structure for this functionality let me know! Thanks!!

UPDATE:
While I would still appreciate input on better possibilities if they exist, this is what I have gotten working now:

class User < ActiveRecord::Base
    # stuff...
    def updated_threads
        MsgThread.find_by_sql("
            SELECT msg_threads.* FROM msg_threads
            INNER JOIN messages ON messages.msg_thread_id = msg_threads.id
            INNER JOIN recipiences ON recipiences.msg_thread_id = msg_threads.id
            WHERE (SELECT COUNT(*) FROM `reads` WHERE reads.message_id = messages.id AND reads.user_id = #{self.id}) = 0
            AND (SELECT COUNT(*) FROM recipiences WHERE recipiences.user_id = #{self.id} AND recipiences.msg_thread_id = msg_threads.id) > 0
        ")
    end
end

Seems to be working fine!

Also to check if a specific thread (and message) are read:

class Message < ActiveRecord::Base
    # stuff...
    def read?(user_id)
        Read.exists?(:user_id => user_id, :message_id => self.id)
    end
end

class MsgThread < ActiveRecord::Base
    # stuff...
    def updated?(user_id)
        updated = false
        self.messages.each { |m| updated = true if !m.read?(user_id)  }
        updated
    end
end

Any suggestions to improve this?

  • 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-14T18:30:23+00:00Added an answer on May 14, 2026 at 6:30 pm

    Add a named_scope to the MsgThread model:

    class MsgThread < ActiveRecord::Base
      named_scope :unread_threads, lambda { |user|
        {
        :include => [{:messages=>[:reads]}, recipiencies],
        :conditions => ["recipiences.user_id = ? AND reads.message_id IS NULL",
                         user.id],
        :group => "msg_threads.id"
        }}    
    end
    

    Note: Rails uses LEFT OUTER JOIN for :include. Hence the IS NULL check works.

    Now you can do the following:

    MsgThread.unread_threads(current_user)
    

    Second part can be written as:

    class Message
      has_many :reads
      def read?(usr)
        reads.exists?(:user_id => usr.id)
      end
    end
    
    class MsgThread < ActiveRecord::Base
      def updated?(usr)
        messages.first(:joins => :reads, 
                       :conditions => ["reads.user_id = ? ", usr.id]
        ) != nil
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm putting together an app that will show a news item's location in Google
Putting together a trivia website where participants only get 30 seconds to see a
In putting together a simple Clock application I discovered that Android requires you to
I've been putting together an auditing solution for a program I am developing, where
I am putting together a php script that pulls html using curl, copies it
I'm putting together somewhat of a social website. It's not another social network, but
I am putting together some codes and attempting to assign a wallpaper with the
I'm putting together a little application that involved both a GUI, HTTP and TCP
I'm putting together my first MVC application and have a question about how to
I am putting together a page displaying a user's tweets whilst visualising the amount

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.