0

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?

Philip7899
  • 4,599
  • 4
  • 55
  • 114

2 Answers2

1

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)
Monideep
  • 2,790
  • 18
  • 19
0

Putting conversation.receipts_for(current_user).update_all(:is_read => true)' in themark_as_read` method worked for me.

def conversation
    if !params[:id] && @activeConvo
      @conversation = @activeConvo
    else
      @conversation ||= mailbox.conversations.find(params[:id])
    end
end
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
sunny
  • 1