简体   繁体   中英

Rails4 - How to delete all messages created using Mailboxer gem?

I followed a tutorial and added messaging functionality in my rails app.
Yesterday, I deleted certain users. Now when I go to my messages link, I get this error message "We're sorry, but something went wrong. If you are the application owner check the logs for more information." which is usually displayed in case of errors.

I saw the log and found out that I must have deleted someone who has sent a message.
I think it's trying to retrieve message of a user that doesn't exist.

ActionView::Template::Error (undefined method `avatar' for nil:NilClass):

How can I solve this issue?
Could anyone please share how to delete all the messages and start fresh? Because the messages are not important.
How do I clear all the mailboxer tables and start fresh? Kindly help.

Here is the log.

2015-05-16T12:17:13.398640+00:00 app[web.1]:   Rendered conversations/_participants.html.erb (17.3ms)
2015-05-16T12:17:13.407905+00:00 app[web.1]:   CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2225]]
2015-05-16T12:17:13.434486+00:00 app[web.1]:    (16.2ms)  SELECT COUNT(*) FROM "mailboxer_receipts" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."id" = "mailboxer_receipts"."notification_id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') WHERE "mailboxer_notifications"."conversation_id" = 973 AND "mailboxer_receipts"."receiver_id" = $1 AND "mailboxer_receipts"."receiver_type" = $2 AND "mailboxer_receipts"."trashed" = 'f' AND "mailboxer_receipts"."is_read" = 'f'  [["receiver_id", 1464], ["receiver_type", "User"]]
2015-05-16T12:17:13.453617+00:00 app[web.1]:   Mailboxer::Receipt Load (1.1ms)  SELECT "mailboxer_receipts".* FROM "mailboxer_receipts" WHERE "mailboxer_receipts"."notification_id" = $1  [["notification_id", 1209]]
2015-05-16T12:17:13.456255+00:00 app[web.1]:   Rendered conversations/_participants.html.erb (20.5ms)
2015-05-16T12:17:13.474657+00:00 heroku[router]: at=info method=GET path="/conversations" host=www.abc.com request_id=991e2d24-c507-4e5f-9b50-5678e2e3aa2c fwd="160.3.94.130" dyno=web.1 connect=1ms service=276ms status=500 bytes=1786
2015-05-16T12:17:13.569003+00:00 heroku[router]: at=info method=GET path="/speak" host=www.abc.com request_id=c1a056f3-4431-4c90-84b8-01c285e606c1 fwd="122.174.14.148" dyno=web.2 connect=1ms service=37ms status=200 bytes=35624
2015-05-16T12:17:13.459247+00:00 app[web.1]:   Mailboxer::Message Load (1.8ms)  SELECT  "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = $1  ORDER BY created_at DESC LIMIT 1  [["conversation_id", 782]]
2015-05-16T12:17:13.469138+00:00 app[web.1]:   Rendered conversations/_conversation.html.erb (233.8ms)
2015-05-16T12:17:13.469485+00:00 app[web.1]:   Rendered conversations/index.html.erb within layouts/application (249.4ms)
2015-05-16T12:17:13.469543+00:00 app[web.1]: Completed 500 Internal Server Error in 261ms
2015-05-16T12:17:13.473924+00:00 app[web.1]: 
2015-05-16T12:17:13.473928+00:00 app[web.1]: ActionView::Template::Error (undefined method `avatar' for nil:NilClass):
2015-05-16T12:17:13.473930+00:00 app[web.1]:     16:       
2015-05-16T12:17:13.473932+00:00 app[web.1]:     17:       <span class="messageuser">
2015-05-16T12:17:13.473933+00:00 app[web.1]:     18:         <%#= conversation.last_message.sender.name %>
2015-05-16T12:17:13.473936+00:00 app[web.1]:     19:         <%= link_to image_tag(conversation.last_message.sender.avatar.url(:mini), class: 'img-rounded'), user_path(conversation.last_message.sender) %>
2015-05-16T12:17:13.473944+00:00 app[web.1]:     20:         <%= link_to conversation.last_message.sender.name, user_path(conversation.last_message.sender) %>
2015-05-16T12:17:13.473946+00:00 app[web.1]:     21:       </span>
2015-05-16T12:17:13.473948+00:00 app[web.1]:     22:       
2015-05-16T12:17:13.473951+00:00 app[web.1]:   app/views/conversations/_conversation.html.erb:19:in `_app_views_conversations__conversation_html_erb___2085506579815819501_69959535266760'
2015-05-16T12:17:13.473952+00:00 app[web.1]:   app/views/conversations/index.html.erb:35:in `_app_views_conversations_index_html_erb___1748297580391940998_69959535151580'
2015-05-16T12:17:13.473954+00:00 app[web.1]: 
2015-05-16T12:17:13.473957+00:00 app[web.1]: 
2015-05-16T12:17:13.572052+00:00 app[web.1]: Started GET "/conversations" for 160.3.94.130 at 2015-05-16 12:17:13 +0000
2015-05-16T12:17:13.579974+00:00 app[web.1]:   User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1464]]
2015-05-16T12:17:13.585203+00:00 app[web.1]:    (0.9ms)  SELECT COUNT(*) FROM "user_learnings" WHERE "user_learnings"."user_id" = $1  [["user_id", 1464]]
2015-05-16T12:17:13.637517+00:00 app[web.1]:    (3.1ms)  SELECT DISTINCT COUNT(DISTINCT "mailboxer_conversations"."id") FROM "mailboxer_conversations" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."conversation_id" = "mailboxer_conversations"."id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') INNER JOIN "mailboxer_receipts" ON "mailboxer_receipts"."notification_id" = "mailboxer_notifications"."id" WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = $1 AND "mailboxer_receipts"."receiver_type" = $2 AND "mailboxer_receipts"."mailbox_type" = $3 AND "mailboxer_receipts"."trashed" = 'f' AND "mailboxer_receipts"."deleted" = 'f'  [["receiver_id", 1464], ["receiver_type", "User"], ["mailbox_type", "inbox"]]
2015-05-16T12:17:13.678884+00:00 app[web.1]:   Mailboxer::Receipt Load (1.2ms)  SELECT "mailboxer_receipts".* FROM "mailboxer_receipts" WHERE "mailboxer_receipts"."notification_id" = $1  [["notification_id", 1443]]
2015-05-16T12:17:13.698778+00:00 app[web.1]:   User Load (4.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1464]]
2015-05-16T12:17:13.464477+00:00 app[web.1]:   User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1937]]

I grew impatient and destroyed them all.

irb(main):002:0> Mailboxer::Conversation.destroy_all

Now the errors are gone and I am able to send messages.
I didn't know how to delete the conversations of 800 users I deleted recently.

You are right. The application is calling methods on users that do not exist. For example: ```

<%= link_to conversation.last_message.sender.name...

(conversation.last_message) is saved, but sender does not exist.

If you want to start the database from scratch you can run (rake db:reset), that will drop create and migrate your database.

To solve the issue create a method that runs every time a user cancels or deletes his/her account, and that it deletes that user's conversations. Try this is your console (rails c), it will delete the first user's conversations. ```

User.first.mailbox.conversations.each do |i| i.delete end

If you still want to show the conversation to the other user who did not canceled his account. You have to save that name somewhere else or show the user that the sender deleted his account. ```

<% if conversation.sender == null %>
  <%= conversation.last_message %>
  <p>User of this conversation deleted his/her account</p>
<%else%>
  <%= conversation.last_message %>
  <%= conversation.last_message.sender.name %>
<% end %>

Sounds like one unoptimized way to do it would be loop through on the model that has messaging functionality added to it with acts_as_messageable ..like MyModel.each do |mod| mod.mailbox.conversations.destroy_all end MyModel.each do |mod| mod.mailbox.conversations.destroy_all end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM