【问题标题】:Messaging gems for rails with the ability to group messages by threadsRails 的消息传递 gem,能够按线程对消息进行分组
【发布时间】:2013-09-05 10:00:05
【问题描述】:

我有一个 rails 3 应用程序。我想添加一个具有以下功能的内部消息传递系统:

  • 用户有很多消息(发送和接收)
  • 要发送消息,用户可以在“收件人”字段中键入一封或多封电子邮件。每封电子邮件都是自动完成的(这不需要在 gem 中)
  • 邮件可以标记为已读、未读和已删除
  • 消息按线程分组:如果 Bob 向 Alice 发送消息,Alice 回复,然后 Bob 再次回复,Bob 只能在他的收件箱中看到此对话的一个条目。 (很像 Gmail)

你知道任何可以帮助我做到这一点的宝石吗?

其中一个最受欢迎的是 Mailboxer (https://github.com/ging/mailboxer)。但我不太喜欢这个模型(通知而不是消息,对话而不是线程,收据表......)。所以我对替代品很感兴趣。

【问题讨论】:

标签: ruby-on-rails-3.2 rubygems messaging


【解决方案1】:

我想建议如下进行关联

​​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

class Message < ActiveRecord::Base
    belongs_to :from_user, :class_name => 'User' # from_user_id field fk Users
    belongs_to :to_user, :class_name => 'User' # to_user_id field fk Users
    belongs_to :thread, :class_name => 'Message' # Reference to parent message
    has_many :replies, :class_name => 'Message', :foreign_key => 'thread_id'
end​​

​您可以按如下方式创建消息

first_msg = Message.new(:to_user => ​userA, :from_user => ​userB, :body => 'Hello!')

​userA​_reply = first_msg.replies.build(:to_user => ​userA, :from_user => ​user​B​, :body => 'hi back')

​userB​_reply = first_msg.replies.build(:to_user => ​user​B​, :from_user => ​userA, :body => 'later')

​并且更容易检查用户发送的消息为@user.messages_sent

并作为@user.messages_received 接收

a) 您可以将https://jqueryui.com/autocomplete/ 用于自动完成电子邮件

b) 您可以使用已读、未读和已删除的布尔字段

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2021-03-27
    • 2016-04-30
    相关资源
    最近更新 更多