Rails私信系统

发布于 2024-10-19 03:52:49 字数 933 浏览 2 评论 0原文

嘿, 我正在尝试实现社交网络中使用的消息系统。我面临的问题首先是关于数据库结构,其次是如何在rails中实现它。
我的第一个想法是我使用 3 个表:

messages: id|subject|text|created_at
接收者:id|message_id|已读:boolean
创建者:id|message_id|read:boolean

现在我想知道如何实现以下功能:
1.) 用户可以删除他的消息。但由于双方都想阅读该消息,如何确保该消息仅在两个用户都删除后才被删除。
2.) 如何实现回复?或者实际上我如何找到相应的创建者?
3.) 如何查明邮件是否被收件人阅读?

另一个想法是: 创建者消息:id|creator_id|receiver_id|主题|文本|已读|created_at receive_messages:与creator_messages相同,

这区分了用户,因此他们可以单独删除他们的消息。但我如何知道邮件是否已被阅读?

我的第三种方法基本上是我的第二种方法,但只有一条表消息,然后将其显示给用户。
1. 一旦用户删除该消息,该消息就会被删除。
2.实际上我如何将关系表示为 has_many 和属于?

我以为它会像这样工作:

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

但不知何故我没有让它工作。我想我在这里缺少一些基本的东西。

希望有人能帮助我=)非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-26 03:52:49

好的,如果我理解正确的话,消息最多有 1 个接收者和 1 个发送者。
在这种情况下,我将执行以下操作:

我将仅创建一个消息模型,这将具有额外的字段
- 接收器读取
- 接收者_已删除
- sender_deleted

现在,您可以在模型中添加“after_save”、“after_create”等钩子,您可以在此处检查receiver_read是否刚刚通过receiver_read_changed设置为true?方法,如果这是真的,您可以通知发件人或用它做其他事情。
通过这个 after_save 钩子,您还可以检查是否 sender_deleted 刚刚设置为 true 并且 receive_deleted 已经为 true ,您将删除整个消息。

当您有多个接收者时,我会为接收者创建一个连接模型,并在消息模型中包含 sender_deleted 和 sender_id 。
在连接模型中,我将添加列receiver_id、已读取和已删除。
现在,我将在消息和连接模型上使用 before_save 方法来检查消息是否需要删除,或者是否必须通知发送者消息已被读取。

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.

远昼 2024-10-26 03:52:49

解决方案可能是:

1)我也会在接收器表中创建一个已删除(_at)标志,如果您确实想从数据库中硬删除消息(如果所有接收器都删除了它),您可以设置一个 cronjob 或其他东西。

2)消息模型中有一个creator_id而不是creators表,我的意思是多人如何创建相同的消息?

3)我真的不明白这个,我猜当用户打开消息时,您将接收器表上的“读”标志设置为“true”,之后您可以在用户模型中创建一个范围,例如“范围” :read, where(:read, true)" 以及未读范围。

我希望这就是你的意思。

The solution might be:

1) I would create a deleted(_at) flag in the receivers table as well, if you really want to hard-remove the messages from the database if all of the receivers deleted it you could setup a cronjob or something.

2) have a creator_id in the messages model instead of a creators table, i mean how can multiple people create the same message?

3) I don't really get this one, i guess you set the "read" flag on the receivers table to "true" when that user opens up the message, after this you can make a scope in the users model like "scope :read, where(:read, true)" and also a scope unread.

I hope this is what you mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文