0

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?

maletor
  • 7,072
  • 7
  • 42
  • 63

2 Answers2

2

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
Ariejan
  • 10,910
  • 6
  • 43
  • 40
  • What does the form look like in the second one? With the first one you just need to create a accepts_nested_attributes_for :recipient right? What does that look like? – maletor Dec 01 '10 at 20:15
0

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

zetetic
  • 47,184
  • 10
  • 111
  • 119