我正在使用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) %>
这是由这个调用执行的:
<% @unread_act.each do |friend| %>
<li><%= render_activity friend %> </li>
<% end %>
该实例变量在我的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
我正在使用 gem Bullet 来查找 N+1 个查询,我得到的反馈是这样的:
N+1 Query detected
Comment => [:node]
Add to your finder: :include => [:node]
N+1 Query method call stack
我最初得到了这个消息 for:trackable
和 for :user
。我现在都渴望通过includes
该赋值语句进行加载。
但是,我不知道如何在急切加载时深入 3 个级别 - 而不仅仅是两个级别。
鉴于该节点被称为 like activity.trackable.node.name
,我想一些适当的急切加载分配看起来像:
@unread_act = Notification.where(owner_id: current_user).includes(:trackable => [:user, :node]).unread_by(current_user)
但我得到的错误是:
ActiveRecord::AssociationNotFoundError at /
Association named 'node' was not found on Node; perhaps you misspelled it?
当我简单地这样做时,我什至得到了:
@unread_act = Notification.where(owner_id: current_user).includes(:trackable => :node).unread_by(current_user)
所以我怀疑这里出了点问题。
有任何想法吗?
编辑 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
Notification.rb
class Notification < PublicActivity::Activity
acts_as_readable :on => :created_at
end