在 rails 3 中模拟用户的消息 [英] model user's message in rails 3

查看:12
本文介绍了在 rails 3 中模拟用户的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了以下模型来处理用户的消息交换:

I have built the following model to handle user's message exchange:

 create_table "messages", :force => true do |t|
    t.integer  "source_id"
    t.integer  "destination_id"
    t.string   "object"
    t.string   "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

这些是它的关联:

class Message < ActiveRecord::Base
  belongs_to :sender, :class_name=>'User', :foreign_key=>'source_id'
  belongs_to :reciever, :class_name=>'User', :foreign_key=>'destination_id'
end

其他这些是另一侧(用户模型)的关联:

And these other are the associations on the other side (the user model):

 has_many :sent_messages, :class_name=> 'Message', :foreign_key=>'source_id', :dependent=>:destroy
  has_many :recieved_messages, :class_name=> 'Message', :foreign_key=>'destination_id', :dependent=>:destroy

模型是正确的,工作正常,实际上我可以从消息中检索到谁是发送者,谁是接收者,从用户那里,我可以获取所有已发送和已接收的消息.不幸的是,它不处理任何情况:如果接收方或发送方删除消息怎么办?该消息是独一无二的,因此它在双方都消失了(坏事).如何知道对方是否已经阅读了消息?有什么建议吗?你认为我必须重新规划模型吗?tnx

The model is correct and work properly, in fact from the message I can retrieve who is the sender and who is the receiver and from the user, I can get all the sent and received messages. Unfortunately, It does not handle any situation: What if the receiver or the sender delete the message ? The message is unique so it disappear in both sides (bad thing). How to know if one of the side had already read the message ? Any suggestion ? Do you think I have to replan the model ? Tnx

推荐答案

这是个好问题!我会将其建模为尽可能与电子邮件模型进行比较.所以一条消息总是属于一个用户,它要么被发送,要么被接收.

this is a nice problem! I would model that to compare as closely as possible to the e-mail model. So a message always belongs to a single user, and it was either sent or received.

简而言之:

 create_table "messages", :force => true do |t|
    t.integer  :user_id
    t.string   :subject
    t.string   :body
    t.boolean  :sent
  end

模型想要:

class Message < ActiveRecord::Base
  belongs_to :user

  scope :sent, where(:sent => true)
  scope :received, where(:sent => false)

end

在用户中:

class User    
  has_many :messages
end

然后您就可以通过

user.messages.sent

和收到的消息

user.messages.received

发送消息确实变得有点复杂:

Sending a message does become a bit more complicated then:

class Message

  def send_message(from, recipients)
    recipients.each do |recipient|
      msg = self.clone
      msg.sent = false
      msg.user_id = recipient
      msg.save
    end
    self.update_attributes :user_id => from.id, :sent => true
  end   
end

或类似的东西:您复制邮件并将其附加到所有收件人,最后将原始邮件设为已发送邮件.

or something along those lines: you copy the message and attach it to all recipients, and lastly make the original message the sent message.

这样每个用户都可以完全控制消息.

This way each user has total control over the message.

可能的改进:

  • 还要在消息中保留对发送者和接收者的明确引用,以便能够允许回复和其他内容
  • 也许允许使用文件夹,而不是使用单个布尔值?

希望这会有所帮助.

这篇关于在 rails 3 中模拟用户的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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