Rails threaded private messaging

牧云@^-^@ 提交于 2019-12-20 09:38:25

问题


I have the following two models:

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.

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?

Thanks.


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/8405457/rails-threaded-private-messaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!