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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:21:48+00:00 2026-05-20T07:21:48+00:00

Hey, I’m trying to implement a message system as used in social networks. The

  • 0

Hey,
I’m trying to implement a message system as used in social networks. The problem I’m facing is first about the database structure and second how to implement it in rails.
My first idea is I’m using 3 tables:

messages: id|subject|text|created_at
receivers: id|message_id|read:boolean
creators: id|message_id|read:boolean

now I’m wondering how to implement following features:
1.) a user can delete his message. but as both want to read the message, how to make sure the message is only deleted when both users have deleted it.
2.) how do I implement a reply? or actually how do I find the corresponding creator?
3.) how to find out whether a mail was read by the receiver?

another idea is:
creator_messages: id|creator_id|receiver_id|subject|text|read|created_at
receiver_messages: same as creator_messages

this distinguishes between the users, so they can delete individually their messages. but how do i find out, whether the mail was read or not?

my third approach was basicly my second but only one table messages and then displaying this to the user.
1. the message is deleted as soon as one of the user deletes it.
2. actually how do I represent the relationships as has_many and belongs to?

I thought it would work like this:

model User
  :has_many :send_messages, :class_name=>"messages", :foreign_key=>"creator_id"
  :has_many :received_messages, :class_name=>"messages", :foreign_key=>"receiver_id"
end

model Messages
  belongs_to :user
end

but somehow I didn’t get it to work. guess I’m missing something basic here.

Hope someone can help me =) thanks a lot

  • 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-20T07:21:48+00:00Added an answer on May 20, 2026 at 7:21 am

    ok, if i understand it correctly the messages would have maximum of 1 receiver and 1 sender.
    In that case i would do the following:

    I would create just a messages model, this would have the extra fields
    – receiver_read
    – receiver_deleted
    – sender_deleted

    Now you can add hooks to the model like “after_save”, “after_create” in here you can check if the receiver_read has just been set to true via for example with the receiver_read_changed? method, if this is true you can notify the sender or do something else with it.
    With this after_save hook you can also check that if the sender_deleted is just set to true and the receiver_deleted is already true you delete the whole message.

    When you have multiple receivers I would create a join model for the receivers and have the sender_deleted and sender_id in the message model.
    In the join model i would add the columns receiver_id, read and deleted.
    Now i would use the before_save method on the messages and the join models to check if the message needs to be deleted or if the sender has to be notified of the message that has been read.

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

Sidebar

Related Questions

Hey so what I want to do is snag the content for the first
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey having some trouble trying to maintain transparency on a png when i create
Hey guys I have about 8 fieldSets and Im iterating over a list. I
Hey I thought about writing a function that prompts me in certain situations (perl
Hey all. To be honest, I don't prefer MS Access as a relational database
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey Folks i am trying to compile this C++ program: #include <stdio.h> #include <string.h>
Hey, I'm currently trying to send an image file to a web server using
Hey all i have an XML file that i am trying to loop though.

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.