问题
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?
回答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)
回答2:
Putting conversation.receipts_for(current_user).update_all(:is_read => true)'
in the
mark_as_read` method worked for me.
def conversation
if !params[:id] && @activeConvo
@conversation = @activeConvo
else
@conversation ||= mailbox.conversations.find(params[:id])
end
end
来源:https://stackoverflow.com/questions/21887598/cannot-figure-out-how-to-update-mailboxer-is-read