Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中的消息传递系统_Ruby On Rails_Forms_Has And Belongs To Many - Fatal编程技术网

Ruby on rails Rails中的消息传递系统

Ruby on rails Rails中的消息传递系统,ruby-on-rails,forms,has-and-belongs-to-many,Ruby On Rails,Forms,Has And Belongs To Many,我想在rails中设置一个白名单消息传递系统,用户可以在其中选择要发送评论的其他用户。信息可能对每个人都可见,也可能仅对一个人可见。我将如何设置它以及消息表单是什么样子的?添加一个只有消息id和收件人id的联接表 class Message has_and_belongs_to_many :recipients end class Recipient has_and_belongs_to_many :messages end m = Message.new m.recipients

我想在rails中设置一个白名单消息传递系统,用户可以在其中选择要发送评论的其他用户。信息可能对每个人都可见,也可能仅对一个人可见。我将如何设置它以及消息表单是什么样子的?

添加一个只有
消息id
收件人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
选项是为每个收件人复制邮件。这是一个很好的解决方案,因为每个收件人都可以完全控制其邮件收件箱(例如,删除邮件)


你也可以看看这个插件。它有点过时,但它解决了您的问题。

第二个表单中的表单是什么样子的?对于第一个,您只需要为收件人创建一个accepts\u嵌套的\u attributes\u,对吗?那是什么样子的?
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