将recipient_deleted id设置为当前用户ID

时间:2013-11-05 19:04:08

标签: ruby-on-rails

我需要帮助将recipient_deleted id设置为我的收件箱邮件系统的用户当前ID。当我删除一条消息时,数据库中'recipient_deleted'列的值从0变为1.我希望该值更改为当前的user_id。例如,如果我与用户28签名并且我从收件箱中删除了一条消息。该邮件的recipient_deleted值应更改为“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

1 个答案:

答案 0 :(得分: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_id实际分配给recipient_deleted

现在想到的是为什么你也需要它?它不在recipient_id字段中吗?因此,如果您将recipient_deleted设置为true,那么您就知道删除它的用户是收件人。

另一个问题:你作为第二个参数传递给user_id方法的mark_message_deleted是从控制器调用它并传入current_user或{{1 }}?