pwz2000
pwz2000

Reputation: 1395

Set recipient_deleted id to the current user id

I need help with setting the recipient_deleted id to users current id for my inbox messaging system. When I deleted a message the value in the database for column 'recipient_deleted' changes from 0 to 1. I want for the value to change to the current user_id. For example if I signon with user 28 and I deleted a message from my inbox. The recipient_deleted value for that message should change to '28'. I have tried to change this around under def mark_message_deleted but nothing I does works. If someone could assist that would be great!

Messages Model:

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

Migration:

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

Upvotes: 1

Views: 66

Answers (1)

DiegoSalazar
DiegoSalazar

Reputation: 13521

You're assigning true to that column:

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

This part:

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

Should be:

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

It's good that you have that migration changing those columns to integer fields, so you're halfway there. You just needed to actually assign the recipient_id to the recipient_deleted.

Now what comes to mind is why do you need it there too? Isn't it already in the recipient_id field? So that if you set recipient_deleted to true then you know the user that deleted it was the recipient.

Another question: the user_id that you're passing in to your mark_message_deleted method as the 2nd argument, are you calling that from the controller and passing in current_user or current_user.id?

Upvotes: 1

Related Questions