ruby-on-railsrubyruby-on-rails-4mailboxer

cannot figure out how to update mailboxer is_read


I am using the mailboxer gem and I am trying to make it so that after i look at a conversation (by accessing conversations#show), I want the is_read attribute of the receipt to turn true. However, the attribute will not turn true until I send a reply. I tried using the following line:

receipt.update_attributes(is_read: true) 

but was returned the following error:

Error (ActiveRecord::ReadOnlyRecord)

I think I understand the error. I think it is saying that the attribute can only be read and not updated. My question is, how do I implement the functionality to have is_Read turn true if i go to the conversations#show page?


Solution

  • Instead of updating the is_read attribute try this

    #conversations_controller.rb
    def show
      @receipts = mailbox.receipts_for(conversation).not_trash
      @receipts.mark_as_read
    end
    
    private
    
    def mailbox
        @mailbox ||= current_user.mailbox
    end
    
    def conversation
        @conversation ||= mailbox.conversations.find(params[:id])
    end
    

    You can also mark a entire conversation as read with

    conversation.mark_as_read(current_user)