如何为多个关联进行急切加载?

mar*_*ion 5 ruby-on-rails query-optimization eager-loading ruby-on-rails-4 public-activity

我正在使用public_activity和我的一个部分我有这个电话:

<%= link_to "#{activity.trackable.user.name} " + "commented on " + "#{activity.trackable.node.name}", node_path(activity.trackable.node.family_tree_id,activity.trackable.node) %>
Run Code Online (Sandbox Code Playgroud)

这是通过这个电话执行的:

      <% @unread_act.each do |friend| %>
        <li><%= render_activity friend %> </li>
      <% end %>
Run Code Online (Sandbox Code Playgroud)

那个实例变量在我ApplicationController这里分配:

  before_filter :handle_nofications

  def handle_nofications
    if current_user.present?
      @unread_act = Notification.where(owner_id: current_user).includes(:trackable => :user).unread_by(current_user)
    end
  end
Run Code Online (Sandbox Code Playgroud)

我正在使用gem bullet,找到N + 1个查询,我得到的反馈是这样的:

N+1 Query detected
  Comment => [:node]
  Add to your finder: :include => [:node]
N+1 Query method call stack
Run Code Online (Sandbox Code Playgroud)

我最初得到了该消息:trackable,并为:user.这两个我现在都渴望通过includes该赋值语句加载.

但是,我不知道如何在热切的加载中达到3级 - 而不仅仅是两级.

鉴于节点被调用activity.trackable.node.name,我想一些适当的预先加载分配看起来像:

  @unread_act = Notification.where(owner_id: current_user).includes(:trackable => [:user, :node]).unread_by(current_user)
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是这样的:

ActiveRecord::AssociationNotFoundError at /
Association named 'node' was not found on Node; perhaps you misspelled it?
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我甚至会这样做:

@unread_act = Notification.where(owner_id: current_user).includes(:trackable => :node).unread_by(current_user)
Run Code Online (Sandbox Code Playgroud)

所以我怀疑其他地方出了问题.

有任何想法吗?

编辑1

请参阅下面的节点和通知模型和关联.

Node.rb

class Node < ActiveRecord::Base
  include PublicActivity::Model
  tracked except: :update, owner: ->(controller, model) { controller && controller.current_user }

  belongs_to :family_tree
  belongs_to :user
  belongs_to :media, polymorphic: true, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :node_comments, dependent: :destroy
Run Code Online (Sandbox Code Playgroud)

Notification.rb

class Notification < PublicActivity::Activity
  acts_as_readable :on => :created_at
end
Run Code Online (Sandbox Code Playgroud)

mar*_*ion 0

解决这个问题的正确方法似乎是在trackable属性上使用数组,如下所示:

@unread_act = Notification.where(recipient_id: current_user).includes(:trackable => [:user, :node]).unread_by(current_user).order("created_at desc")
Run Code Online (Sandbox Code Playgroud)

主要部分是:

includes(:trackable => [:user, :node])

这看起来效果很好。