受信トレイ メッセージング システムのユーザーの現在の ID に recipient_deleted 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