1

我需要帮助将接收者删除的 ID 设置为我的收件箱消息系统的用户当前 ID。当我删除一条消息时,数据库中“recipient_deleted”列的值从 0 更改为 1。我希望该值更改为当前的 user_id。例如,如果我使用用户 28 登录并从收件箱中删除了一条消息。该邮件的接收者已删除值应更改为“28”。我试图改变这一点,def mark_message_deleted但我没有做任何事情。如果有人可以提供帮助,那就太好了!

消息模型:

attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
        validates_presence_of :subject, :message => "Please enter message title"
        has_many :notifications, as: :event
  scope :unread, -> {where('read_at IS NULL')}
  scope :not_deleted_by_recipient, where('messages.recipient_deleted IS NULL OR messages.recipient_deleted = ?', false)
  scope :not_deleted_by_sender, where('messages.sender_deleted IS NULL OR messages.sender_deleted = ?', false)


        belongs_to :sender,
        :class_name => 'User',
        :foreign_key => 'sender_id'
        belongs_to :recipient,
        :class_name => 'User',
        :foreign_key => 'recipient_id'


    # marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
    # When both sender and recipient marks it deleted, it is destroyed.
    def mark_message_deleted(id,user_id)
         self.sender_deleted = true if self.sender_id == user_id
         self.recipient_deleted = true if self.recipient_id == user_id
         (self.sender_deleted && self.recipient_deleted) ? self.destroy : self.save!
     end
    # Read message and if it is read by recipient then mark it is read
    def readingmessage
      self.read_at ||= Time.now
      save
    end

    # Based on if a message has been read by it's recipient returns true or false.
    def read?
            self.read_at.nil? ? false : true
    end


    def self.received_by(user)
       where(:recipient_id => user.id)
     end


     def self.not_recipient_deleted
       where("recipient_deleted = ?", false)
     end

     def self.sent_by(user)
        Message.where(:sender_id => user.id)
      end

      def next(same_recipient = true)
        collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC')
        collection.where(recipient_id: self.recipient_id) if same_recipient
        collection.first
      end


      def previous(same_recipient = true)
        collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC')
        collection.where(recipient_id: self.recipient_id) if same_recipient
        collection.first
      end
    end

    private
    def send_notification(message)
      message.notifications.create(user: message.recipient)
    end

移民:

class ChangeRecipientdeletedAndSenderdeletedFormatInMyTable < ActiveRecord::Migration
      def self.up
        change_column :messages, :recipient_deleted, :integer
         change_column :messages, :sender_deleted, :integer
      end


      def self.down
        change_column :messages, :recipient_deleted, :boolean
        change_column :messages, :sender_deleted, :boolean

      end
    end
4

1 回答 1

1

您正在分配true给该列:

def mark_message_deleted(id,user_id)
  self.sender_deleted = true if self.sender_id == user_id
  self.recipient_deleted = true if self.recipient_id == user_id
  (self.sender_deleted && self.recipient_deleted) ? self.destroy : self.save!
end

这部分:

self.recipient_deleted = true if self.recipient_id == user_id

应该:

self.recipient_deleted = user_id if self.recipient_id == user_id

迁移将这些列更改为integer字段是一件好事,这样您就完成了一半。您只需要将 实际分配recipient_idrecipient_deleted.

现在想到的是为什么你也需要它?不是已经在recipient_id现场了吗?因此,如果您设置recipient_deletedtrue然后您知道删除它的用户是收件人。

另一个问题:user_idmark_message_deleted作为第二个参数传入你的方法,你是从控制器调用它并传入current_userorcurrent_user.id吗?

于 2013-11-05T19:31:50.243 回答