ruby-on-rails – 实现一个facebook风格的消息系统,用户可以在另一个模型的两个不同列中

我正在尝试建立一个类似于facebook的消息系统,你有一个按两个用户间的对话排序的消息列表(这对于目前的多个收件人来说并不重要,但也许如果我使用更智能的设计那么它可以将来很容易实现.我认为添加到我现在的东西并不容易.)我有一些工作,但我无法实现一些功能.我对rails / web编程很新,因此非常感谢任何和所有提示/提示/解决方案.

所以我有三个相关的模型:User,Conversation和Messages.用户有很多会话,Conversation有很多消息,属于Users,Message属于Conversation.好的,这就是模型的样子:

用户:具有相关字段ID:int,username:string

class User < ActiveRecord::Base

  has_many :conversations,:class_name => "Conversation",:finder_sql =>
            proc { 
            "SELECT * FROM conversations " +
            "WHERE conversations.user1_id = #{id} OR conversations.user2_id = #{id} " +
            "ORDER BY conversations.updated_at DESC" }

会话:有相关字段:ID:int,user1_id:int,user2_id:int,user1_deleted:boolean,user2_deleted:boolean,created_at:datetime,updated_at:datetime

class Conversation < ActiveRecord::Base
  has_many :messages  

  belongs_to :participant_one,:class_name => "User",:foreign_key => :user1_id
  belongs_to :participant_two,:foreign_key => :user2_id

  private

    def self.between(user1,user2)
      c = Conversation.arel_table
      Conversation.where(c[:user1_id].eq(user1).and(c[:user2_id].eq(user2)).or(c[:user1_id].eq(user2).and(c[:user2_id].eq(user1))))

消息:具有相关字段:id:int,conversation_id:int,author_id:int,content:text,updated_at:datetime

class Message < ActiveRecord::Base
  belongs_to :conversation,:touch => true

我不确定我是否需要participant_one和participant_two,但我使用了

def conversation_partner(conversation)
    conversation.participant_one == current_user ? conversation.participant_two : conversation.participant_one
  end

在ConversationHelper中,以便在视图中,我可以显示其他参与者.

所以这基本上有效.但我遇到的一个复杂问题是我在Conversation中并没有真正区分用户,用户可以在user1字段或user2字段中.所以我需要不断地在一个或另一个领域中寻找用户,例如在用户has_many声明的finder_sql中.此外,当我创建新消息时,我首先搜索是否存在Conversation参数,或者如果没有,则查看两个用户之间是否有对话,如果没有,则创建新对话. (您可以从会话索引发送消息(如回复),或者current_user可以查看另一个用户并单击“向该用户发送消息”链接.messagecontroller看起来像这样,并使用该自我.在Conversation模型中的方法之间:

class MessagesController < ApplicationController
  before_filter :get_user
  before_filter :find_or_create_conversation,:only => [:new,:create]

  def new
     @message = Message.new
  end

  def create
    @message = @conversation.messages.build(params[:message])
    @message.author_id = current_user.id
    if @message.save
      redirect_to user_conversation_path(current_user,@conversation),:notice => "Message sent!"
    else
      redirect_to @conversation
    end
  end

  private
     def get_user
      @user = User.find(params[:user_id])
    end

    def find_or_create_conversation
      if params[:conversation_id]
        @conversation = Conversation.find(params[:conversation_id]) 
      else
        @conversation = Conversation.between(@user.id,current_user.id).first or @conversation = Conversation.create!(:user1_id => current_user.id,:user2_id => @user.id)
      end
    end

(我的路线看起来像这样:)

resources :users do
    resources :conversations,:only => [:index,:create,:show,:destroy] do
      resources :messages,:create]
    end
    resources :messages,:only => [:new]
  end

所以现在,我在尝试设置user1_deleted或user2_deleted标志时遇到问题. (同样,如果/当我实现读取/最新标志时).问题是因为同一个用户可以有很多对话,但他可以是user1或user2,很难找到他.我以为我可以在Conversation模型中做这样的事情:

def self.active(user)
  Conversation.where(which_user?(user) + "_deleted = ?",false)
end

def self.which_user?(user)
  :user1_id == user ? 'user1' : 'user2'
end

但是,除非你逐个遍历用户的每个对话,否则你无法运行整个对话,因为有时他是user1,有时候他是user2.我应该抛弃这整个方法并尝试新的设计吗?如果是这样,有没有人可能更优雅/更好/实际工作并仍然满足相同的需求?

这是一个很长的问题,所以我很欣赏任何愿意和我一起讨论这一切的人.谢谢.

解决方法

kindofgreat,

这个问题引起了我的兴趣,所以我花了几个小时进行实验,这是我的发现.结果是一个应用程序,任何数量用户都可以参与对话.

我选择了一个数据模型,它在User和Conversation之间有一个名为UserConveration的中间模型;它是一个连接模型,一起保存有关用户状态和对话的数据(即,是否读取,删除对话等)

实现在GitHub上,你可以看到我写的代码的差异(相对于自动生成代码,以保持所有的残余)在https://github.com/BinaryMuse/so_association_expirement/compare/53f2263…master.

这是我的模型,仅限于协会:

class User < ActiveRecord::Base
  has_many :user_conversations
  has_many :conversations,:through => :user_conversations
  has_many :messages,:through => :conversations
end

class UserConversation < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
  has_many :messages,:through => :conversation

  delegate :subject,:to => :conversation
  delegate :users,:to => :conversation
end

class Conversation < ActiveRecord::Base
  has_many :user_conversations
  has_many :users,:through => :user_conversations
  has_many :messages
end

class Message < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end

这是数据库的样子:

create_table "conversations",:force => true do |t|
  t.string   "subject"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "messages",:force => true do |t|
  t.integer  "user_id"
  t.integer  "conversation_id"
  t.text     "body"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "user_conversations",:force => true do |t|
  t.integer  "user_id"
  t.integer  "conversation_id"
  t.boolean  "deleted"
  t.boolean  "read"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "users",:force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

基本思想是向用户呈现“对话”,而实际幕后我们正在为对话中涉及的所有用户管理UserConversations.尤其请参阅UserConversation模型上的方法create_user_conversations,该模型负责为与会话关联的每个用户在连接表中创建条目.

在模型中还有很多has_many:through和delegates调用,以尽可能轻松地获取我们想要的数据…例如而不是@ user_conversation.conversation.subject你可以使用@ user_conversation.subject;对于messages属性也是如此.

我意识到这是相当多的代码,所以我鼓励你获取代码并使用它.这一切都有效,除了“删除”对话(我没有打扰这个,但将消息标记为已读/未读确实有效).请注意,您必须作为用户登录”以执行某些操作,例如创建新会话等.您可以通过从主页面单击用户并选择“以此用户身份登录”来登录.

要记住的另一件事是,即使正在使用的控制器是“UserConversations”控制器,URL也会说“对话”以保证用户的好处 – 检查路由文件.

如果您有任何更深入/更深入的问题,请随时通过GitHub或我的StackOverflow配置文件上的联系方式与我联系.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...