I use public_activity , and one of my partitions I have this call:
<%= link_to "#{activity.trackable.user.name} " + "commented on " + "#{activity.trackable.node.name}", node_path(activity.trackable.node.family_tree_id,activity.trackable.node) %>
Performed by this call:
<% @unread_act.each do |friend| %> <li><%= render_activity friend %> </li> <% end %>
This instance variable is assigned in my ApplicationController here:
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
I use gem Bullet to find N + 1 requests, and the feedback I get is this:
N+1 Query detected Comment => [:node] Add to your finder: :include => [:node] N+1 Query method call stack
I initially received this message for :trackable and for :user . Both of which I now want to load through includes in this assignment statement.
However, I do not know how to go 3 levels in depth from impatient loading - and not just from two.
Given that node is called as activity.trackable.node.name , I assume that some suitable boot destination would look something like this:
@unread_act = Notification.where(owner_id: current_user).includes(:trackable => [:user, :node]).unread_by(current_user)
But the error I get is this:
ActiveRecord::AssociationNotFoundError at / Association named 'node' was not found on Node; perhaps you misspelled it?
I even get this when I just say:
@unread_act = Notification.where(owner_id: current_user).includes(:trackable => :node).unread_by(current_user)
Therefore, I suspect that something is wrong here.
Any ideas?
Change 1
See below node and notification models and associations.
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