Rails4 - 如何删除使用 Mailboxer gem 创建的所有消息?

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

我按照教程在我的 rails 应用程序中添加了消息传递功能。
昨天,我删除了某些用户。现在,当我转到消息 link 时,我收到此错误消息 "We're sorry, but something went wrong. If you are the application owner check the logs for more information.",通常在出现错误时显示。

我看了日志,发现一定是我删除了发消息的人
我认为它正在尝试检索不存在的用户的消息。

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

我该如何解决这个问题?
谁能分享一下如何删除所有消息并重新开始?因为消息并不重要。
如何清除所有邮箱表并重新开始?请帮忙。

这是日志。

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" =  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" =  AND "mailboxer_receipts"."receiver_type" =  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" =   [["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" =   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" =  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" =   [["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" =  AND "mailboxer_receipts"."receiver_type" =  AND "mailboxer_receipts"."mailbox_type" =  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" =   [["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" =  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" =  LIMIT 1  [["id", 1937]]

听起来像是一种未优化的方法是在模型上循环,该模型使用 acts_as_messageable 添加了消息传递功能..like MyModel.each do |mod| mod.mailbox.conversations.destroy_all end

你是对的。应用程序正在调用不存在的用户的方法。例如: ```

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

(conversation.last_message)已保存,但发件人不存在。

如果您想从头开始创建数据库,您可以 运行 (rake db:reset),这将放弃创建和迁移您的数据库。

要解决此问题,请创建一个方法,在用户每次取消或删除 his/her 帐户时 运行s,并删除该用户的对话。 尝试这是您的控制台 (rails c),它会删除第一个用户的对话。 ```

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

如果您仍想向未注销帐户的其他用户显示对话。您必须将该名称保存在其他地方或向用户显示发件人删除了他的帐户。 ```

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

我不耐烦了,把它们都毁了。

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

现在错误消失了,我可以发送消息了。
最近删了800个用户的对话不知道怎么删