Rare streaming private messages

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 responses must be numbered from the original message.

In my "index" controller action, how do I request 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 do this with my current structure?

Thank.

+5
source share
1 answer

, , . :

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

-, , users.

, , , , .

/, , , , .

, Ruby Rails, Thread , .

+16

All Articles