Eager Loading Help

46 views
Skip to first unread message

yellowreign

unread,
Jul 6, 2017, 5:50:21 PM7/6/17
to Public Activity
I'm using Public Activity with my Rails app.  I have an `activities#index` that shows activities related to them.  In this view, I'm getting N+1 warnings from the Bullet gem.

I'm receiving these messages:

    user: Foo
      USE eager loading detected
     
Comment => [:product]
     
Add to your finder: :includes => [:product]


    user
: Foo
      USE eager loading detected
     
CollectionProduct => [:collection]
     
Add to your finder: :includes => [:collection]


    user
: Foo
      USE eager loading detected
     
CollectionProduct => [:product]
     
Add to your finder: :includes => [:product]


I found this question

However, when I tried adjusting my query (added the part after `trackable`), like they did:
@activities = current_user.activities_feed.includes(:owner,:trackable=> [:product, :collection])


I receive an error like this:
Association named 'product' was not found on Collection; perhaps you misspelled


If I change the query to use `:products`, it fixes the error above, but I receive a different error (since 'Comment' belongs to 'product'):
Association named 'products' was not found on Comment; perhaps you misspelled it?


Based on the Rails Guide on Active Record Queries (Eager Loading section), I tried this:
@activities = current_user.activities_feed.includes(:owner,:trackable=> [{comment: :product}, {collection_product: :collection}, {collection_product: :product}]  )




But I received this error:
Association named 'comment' was not found on Collection; perhaps you misspelled it?


my controller:
@activities = current_user.activities_feed.includes(:owner,:trackable)


my view:
<%= custom_render(activity, @item) %>


activity helper:

    def custom_render(activity, scope = nil)
     
if activity.recipient_id == current_user.id
       
if activity.key == "friendship.create"
          activity
.key = "activity.friendship.recipient"
       
elsif activity.key == "comment.upvote"
          activity
.key = "activity.comment.upvote_recipient"
       
elsif activity.key == "collection.upvote"
          activity
.key = "activity.collection.upvote_recipient"
       
end
     
end
      render_activity
(activity)
   
end


The models mentioned above.  I think that the problem could be in the `User``activities_feed`method.

 
   class User < ActiveRecord::Base
     
def activities_feed
        collections_ids
= get_voted(Collection).pluck(:id)
         
PublicActivity::Activity.order("created_at desc").
           
where("owner_id in (?) AND owner_type = ? AND (trackable_type = ? OR trackable_type = ? OR trackable_type = ?)", friend_ids, "User", "Product", "Collection", "Comment").
           
or(PublicActivity::Activity.order("created_at desc").
           
where("recipient_id = ? AND (key = ? OR key = ? OR key = ?)", id, "friendship.create", "comment.upvote", "collection.upvote")).
           
or(PublicActivity::Activity.order("created_at desc").
           
where("key = ? AND recipient_id in (?)", "collection_product.create", collections_ids))
     
end
   
end    


   
class Comment < ActiveRecord::Base
      include
PublicActivity::Common
      belongs_to
:product
      has_many
:activities, as: :trackable, class_name: 'PublicActivity::Activity', dependent: :destroy
   
end


   
class Product < ActiveRecord::Base
      include
PublicActivity::Common
      has_many
:comments, dependent: :destroy
      has_many
:collection_products, dependent: :destroy
      has_many
:collections, through: :collection_products
      has_many
:activities, as: :trackable, class_name: 'PublicActivity::Activity', dependent: :destroy
   
end


   
class Collection < ActiveRecord::Base
      include
PublicActivity::Common
      has_many
:collection_products, dependent: :destroy
      has_many
:products, through: :collection_products
      has_many
:activities, as: :trackable, class_name: 'PublicActivity::Activity', dependent: :destroy
   
end


   
class CollectionProduct < ActiveRecord::Base
      include
PublicActivity::Common
      belongs_to
:collection, touch: true
      belongs_to
:product
      has_many
:activities, as: :trackable, class_name: 'PublicActivity::Activity', dependent: :destroy
   
end



Reply all
Reply to author
Forward
0 new messages