Rails线程私有消息 [英] Rails threaded private messaging

查看:162
本文介绍了Rails线程私有消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两种模式:

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.

在我的索引控制器操作中,如何查询已发送的消息和收到的消息?例如,如果User1命中'/ users / 2 / messages /',他们应该看到user1和user2之间的整个对话(无论谁发送了第一条消息)。我是否需要添加线程模型,或者有没有办法用我当前的结构来完成这个?

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?

谢谢。

推荐答案

您可能最好将此重组为可以加入人员的对话,而不是链中的一系列互连消息。例如:

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.

可以通过在Message本身或使用祖先建立父关系来添加线程消息传递,但实际上这往往会过度杀死,因为回复的简单时间顺序通常对大多数人来说已经足够了。

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.

请记住,某些名称是由Ruby或Rails保留的, Thread 是其中之一,所以你不能拥有具有该名称的模型。

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.

这篇关于Rails线程私有消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆