简体   繁体   English

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

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

I followed a tutorial and added messaging functionality in my rails app. 我在rails应用程序中遵循了教程并添加了消息传递功能。
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. 我不知道如何删除最近删除的800个用户的对话。

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. (conversation.last_message)已保存,但发件人不存在。

If you want to start the database from scratch you can run (rake db:reset), that will drop create and migrate your database. 如果要从头开始启动数据库,可以运行(rake db:reset),这将删除创建和迁移数据库。

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. 试试这是你的控制台(rails c),它将删除第一个用户的对话。 ``` ```

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 听起来像一个未经优化的方法,它将循环通过使用acts_as_messageable添加消息传递功能的模型。像MyModel.each do |mod| mod.mailbox.conversations.destroy_all end MyModel.each do |mod| mod.mailbox.conversations.destroy_all end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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