cannot figure out how to update mailboxer is_read

耗尽温柔 提交于 2019-12-25 03:00:54

问题


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 themark_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!