检测到邮箱N + 1查询

时间:2017-07-20 04:22:49

标签: ruby-on-rails mailboxer

我设法用this tutorial安装邮箱,但我有一个反复出现的错误:

N+1 Query detected
  Mailboxer::Message => [:sender]
  Add to your finder: :includes => [:sender]
N+1 Query method call stack
  app/views/conversations/_messages.html.erb:12:in `block in _app_views_conversations__messages_html_erb__3829465222244059655_69982701059040'
  app/views/conversations/_messages.html.erb:1:in `_app_views_conversations__messages_html_erb__3829465222244059655_69982701059040'
  app/views/conversations/show.html.erb:27:in `_app_views_conversations_show_html_erb__1439517360344897040_69982700516580'

  app/views/conversations/_messages.html.erb:12:in `block in _app_views_conversations__messages_html_erb__3829465222244059655_69982701059040'
  app/views/conversations/_messages.html.erb:1:in `_app_views_conversations__messages_html_erb__3829465222244059655_69982701059040'
  app/views/conversations/show.html.erb:27:in `_app_views_conversations_show_html_erb__1439517360344897040_69982700516580'

我有另一个用于:消息,但我用包含修复了问题。如果我尝试使用:发送者,我有这个错误:

Association named 'sender' was not found on Mailboxer::Receipt; perhaps you misspelled it?

教程原始代码:conversations_controller.rb

class ConversationsController < ApplicationController
  before_action :authenticate_user! 

  def new
  end

  def create
    recipients = User.where(id: conversation_params[:recipients])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:success] = "Your message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def show
    @receipts = conversation.receipts_for(current_user)
    # mark conversation as read
    conversation.mark_as_read(current_user)
  end

  def reply
    current_user.reply_to_conversation(conversation, message_params[:body])
    flash[:notice] = "Your reply message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def trash
    conversation.move_to_trash(current_user)
    redirect_to mailbox_inbox_path
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to mailbox_inbox_path
  end

  private

  def conversation_params
    params.require(:conversation).permit(:subject, :body,recipients:[])
  end

  def message_params
    params.require(:message).permit(:body, :subject)
  end
end

教程原始代码:show.html.erb

<div class="row">
  <div class="spacer"></div>
    <div class="col-md-6">
    <%= link_to "Compose", new_conversation_path, class: "btn btn-success" %>
    </div>
    <div class="col-md-6 text-right">
    <% if conversation.is_trashed?(current_user) %>
        <%= link_to 'Untrash', untrash_conversation_path(conversation), class: 'btn btn-info', method: :post %>
    <% else %>
        <%= link_to 'Move to trash', trash_conversation_path(conversation), class: 'btn btn-danger', method: :post,
                    data: {confirm: 'Are you sure?'} %>
    <% end %>
    </div>
  </div>

  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-body">
        <%= render 'mailbox/folders' %>
      </div>
    </div>
  </div>

  <div class="col-md-8">
    <div class="panel panel-default">
      <div class="panel-body">
        <%= render partial: 'messages' %>
      </div>
      <div class="panel-footer">
        <!-- Reply Form -->
        <%= form_for :message, url: reply_conversation_path(conversation) do |f| %>
            <div class="form-group">
              <%= f.text_area :body, placeholder: "Reply Message", rows: 4, class: "form-control" %>
            </div>
            <%= f.submit "Reply", class: 'btn btn-danger pull-right' %>
        <% end %>
        <div class="clearfix"></div>
      </div>
    </div>
  </div>

</div>

教程原始代码:_messages.html.erb

<% @receipts.each do |receipt| %>
    <% message = receipt.message %>
    <div class="media">
      <div class="media-left">
        <!-- user avators can go here -->
        <a href="#">
          <img class="media-object" src="http://placehold.it/64x64" alt="...">
        </a>
      </div>
      <div class="media-body">
        <h4 class="media-heading">
          <%= message.sender.username %> <br>
          <small><b>Subject: </b><%= message.subject %></small><br>
          <small><b>Date: </b><%=  message.created_at.strftime("%A, %b %d, %Y at %I:%M%p") %></small>
        </h4>
        <%= message.body %>
      </div>
    </div>
<% end %>

如果我删除&lt;%= message.sender.username%&gt;问题解决了......

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

通常,要修复这些N+1 query problems,我们使用includes并通过传递哈希,我们可以includes嵌套关联。因此,查看此代码时,调用receipt.message.sender会触发错误,因此我们会有一个Receipt模型,一个message关联以及与之关联的sender。因此,如果我们可以找到加载Receipt的位置,我们可以添加includes(message: :user),就像我们对任何其他模型一样。

深入mailboxer gem,receipts_for操作中的show方法只是Mailboxer::Receipt上几个范围的包装。由于此方法只是为您运行了一些范围,因此我们可以链接到它的末尾,就好像它是一个普通的ActiveRecord where链。

因此,考虑到所有这些,我们应该能够添加includes并避免N + 1查询问题,最终会出现类似

的内容
@receipts = conversation.receipts_for(current_user).includes(message: :sender)