在 Rails 3 中进行私人消息建模的首选方式 [英] Preferred way to private messages modeling in Rails 3

查看:32
本文介绍了在 Rails 3 中进行私人消息建模的首选方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划在成员之间实施一个私人消息系统.我想知道对此的首选方法是什么.

I plan to implement a private message system between members. I'm wondering what are the preferred approaches to this.

要求是

  1. 我应该能够像这样轻松检索它们

  1. I should be able to retrieve them easily as something like this

@user.conversations               #Should return User objects that I sent or received messages from (but not me)
@user.conversations.messages      #Messages from all or specific user objects.
@user.conversations.messages.unread      #Unread messages

  • 当调用@user.conversations 时,应该只检索向我发送消息的人或我向其发送消息的人.应排除 current_user.

  • When calling @user.conversations should retrieve only the people that sent me messages or people I send messages to. current_user should be excluded.

    如果我是 sender_id=5 并且发送 to_id=10,那么对方会回复为 sender=10 to_id=5.这应该被视为和理解为同一个会话对象.

    If i'm sender_id=5 and send to_id=10 then, the other person would reply as sender=10 to_id=5. This should be considered and understood as the same conversation object.

    <小时>

    关于最后一点.我不确定建模的首选方法是什么.


    Regarding last point. I'm not sure what's the preferred approach to modeling.

    最好使用一种对话模型来处理所有消息,例如

    It's preferred to use one Conversation model to handle all messages such as

        attr_accessible :user_id, :friend_id, :message, :read
        belongs_to :user
    

    或者最好创建一个 Conversation 模型来处理关联和一个 Message 模型来处理消息.

    Or it's preferred to create a Conversation model to handle association and a Message model for messages.

    我想看看如何实现这种关系的示例案例,以及是否有其他方法可以实现.

    I would like to see sample cases of how to implement this relationship and if there's additional method to implement.

    我有点迷失在这里.

    推荐答案

    一个更简单的模型是捕获每个消息:

    A much simpler model is to capture each Message:

    class Message < ActiveRecord::Base
      belongs_to :from_user,  :class_name => 'User' # from_user_id field fk Users
      belongs_to :to_user,    :class_name => 'User' # to_user_id field fk Users
      belongs_to :thread, :class_name => 'Message'  # Reference to parent message
      has_many :replies,  :class_name => 'Message', :foreign_key => 'thread_id'
    
      named_scope :in_reply_to, lambda { |message| :conditions => {:thread => message}, :order => 'created_at' }
    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
    

    如果您需要捕获消息线程,任何作为回复的消息都可以存储对开始对话(又名线程)的初始消息的引用.例如:

    If you need to capture the message threads, any Message that's a reply can store a reference to the initial message starting the Conversation (aka Thread). For example:

    first_msg   = Message.new(:to_user => bob, :from_user => sally, :body => 'Hello!')
    sally_reply = first_msg.replies.build(:to_user => bob, :from_user => sally, :body => 'hi back')
    bob_reply   = first_msg.replies.build(:to_user => sally, :from_user => bob, :body => 'later')
    

    这篇关于在 Rails 3 中进行私人消息建模的首选方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆