Rails 中的消息系统

发布于 2024-10-05 23:25:16 字数 87 浏览 2 评论 0原文

我想在 Rails 中设置一个白名单消息系统,用户可以选择他们想要发送评论的其他用户。该消息可能对所有人或只有一个人可见。我该如何设置以及消息表单会是什么样子?

I want to set up a whitelist messaging system in rails where users can select which other users they want to send the comment. The message could be visible to everyone or just one person. How would I set this up and what would the message form look like?

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

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

发布评论

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

评论(2

望笑 2024-10-12 23:25:16

添加仅包含 message_idrecipient_id 的联接表。

class Message
  has_and_belongs_to_many :recipients
end

class Recipient
  has_and_belongs_to_many :messages
end

m = Message.new
m.recipients = list_of_recipients
m.save

选项是为每个收件人复制消息。这是一个很好的解决方案,每个收件人都可以完全控制他们的邮件收件箱(例如删除邮件)。

class Message
  belongs_to :recipient

  def self.post_message(recipients, text)
     recipients.each { |r| Message.create(:recipient => r, :text => text) }
  end
end

class Recipient
  has_many :messages
end

Either add a join table with only a message_id and recipient_id.

class Message
  has_and_belongs_to_many :recipients
end

class Recipient
  has_and_belongs_to_many :messages
end

m = Message.new
m.recipients = list_of_recipients
m.save

Option is duplicating the message for each recipient. This is a great solution is each recipient has full control over their message inbox (e.g. delete the message).

class Message
  belongs_to :recipient

  def self.post_message(recipients, text)
     recipients.each { |r| Message.create(:recipient => r, :text => text) }
  end
end

class Recipient
  has_many :messages
end
花桑 2024-10-12 23:25:16

您还可以查看 acts_as_messageable 插件。它有点过时了,但它解决了您的担忧。

You might also take a look at the acts_as_messageable plugin. It's a bit out of date, but it addresses your concerns.

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