Philip7899
Philip7899

Reputation: 4677

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?

Upvotes: 0

Views: 402

Answers (2)

sunny
sunny

Reputation: 1

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

Upvotes: 0

Monideep
Monideep

Reputation: 2810

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)

Upvotes: 1

Related Questions