How can I upload multiple associations?

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 
+5
source share
2 answers

This seems to be the correct approach using the array in the trackable attribute, for example:

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

Main part:

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

It seems to have worked perfectly.

0
source

This, I hope, will be fixed in rails 5.0. There is already a problem and a transfer request for this.

https://github.com/rails/rails/pull/17479

https://github.com/rails/rails/issues/8005

I have branched rails and applied the patch to 4.2-stable and it works fine for me. Feel free to use it, although I can not guarantee synchronization with the upstream on a regular basis.

https://github.com/ttosch/rails/tree/4-2-stable

+1
source

Source: https://habr.com/ru/post/1206235/


All Articles