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

查看:85
本文介绍了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

您将可以简单地通过以下方式查询所有已发送的邮件

You would then simply be able to query all sent messages by

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天全站免登陆