Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to eager load polymorphic association's associations?

artists have many activities (basically a cache of interactions between users):

class Activity < ActiveRecord::Base

  belongs_to :receiver, :class_name => 'Artist', :foreign_key => :receiver_id #owns the stuff done "TO" him
  belongs_to :link, :polymorphic => true
  belongs_to :creator, :class_name => 'Artist', :foreign_key => :creator_id #person who initiated the activity

end

For example:

  Activity.create(:receiver_id => author_id, :creator_id => artist_id, :link_id => id, :link_type => 'ArtRating') 

I want to create an activity stream page for each artist, consisting of a list of different types of events, ArtRatings (likes, dislikes), Favoriting, Following etc.

The controller looks like this:

class ActivityStreamController < ApplicationController
  def index
    @activities = @artist.activities.includes([:link,:creator,:receiver]).order("id DESC").limit(30)
  end
end

The db call correctly eagerly loads the polymorphic link objects:

  SELECT "activities".* FROM "activities" WHERE (("activities"."receiver_id" = 6 OR "activities"."creator_id" = 6)) ORDER BY id DESC LIMIT 30
  ArtRating Load (0.5ms)  SELECT "art_ratings".* FROM "art_ratings" WHERE "art_ratings"."id" IN (137, 136, 133, 130, 126, 125, 114, 104, 103, 95, 85, 80, 73, 64)
  SELECT "follows".* FROM "follows" WHERE "follows"."id" IN (14, 10)
  SELECT "favorites".* FROM "favorites" WHERE "favorites"."id" IN (25, 16, 14)

But when I display each ArtRating, I also need to reference the post title, which belongs to a post. In the view, if I do:

activity.link.post

It does a separate DB call for each art_rating's post. Is there a way to eagerly load the post as well?

UPDATE TO THE QUESTION:

If there is no way to achieve eager loading of posts using 'includes' syntax, is there a way to manually do the eager loading query myself and inject it into the @activities object?

I see in the DB log:

SELECT "art_ratings".* FROM "art_ratings" WHERE "art_ratings"."id" IN (137, 136, 133, 130, 126, 125, 114, 104, 103, 95, 85, 80, 73, 64)

Is there a way I can access this list of ids from the @activities object? If so, I could do 2 additional queries, 1 to get the art_ratings.post_id(s) in that list, and another to SELECT all posts IN those list of post_ids. Then somehow inject the 'post' results back into @activities so that it's available as activity.link.post when I iterate through the collection. Possible?

like image 912
Homan Avatar asked Dec 28 '11 05:12

Homan


1 Answers

TL;DR my solution makes artist.created_activities.includes(:link) eager load everything you want

Here's my first attempt at it: https://github.com/JohnAmican/music

A few notes:

  • I'm relying on default_scope, so this isn't optimal.
  • It looks like you're using STI. My solution doesn't. That means you can't simply call activities on an artist; you have to reference created_activities or received_activities. There might be a way around this. I'll update if I find anything.
  • I changed some names around because it was confusing to me otherwise.

If you go into console and do created_activities.includes(:link), the appropriate stuff gets eager-loaded:

irb(main):018:0> artist.created_activities.includes(:link)
  Activity Load (0.2ms)  SELECT "activities".* FROM "activities" WHERE "activities"."creator_id" = ?  [["creator_id", 1]]
  Rating Load (0.3ms)  SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" IN (1)
  RatingExplanation Load (0.3ms)  SELECT "rating_explanations".* FROM "rating_explanations" WHERE "rating_explanations"."rating_id" IN (1)
  Following Load (0.3ms)  SELECT "followings".* FROM "followings" WHERE "followings"."id" IN (1)
  Favorite Load (0.2ms)  SELECT "favorites".* FROM "favorites" WHERE "favorites"."id" IN (1)
=> #<ActiveRecord::Relation [#<Activity id: 1, receiver_id: 2, creator_id: 1, link_id: 1, link_type: "Rating", created_at: "2013-10-31 02:36:27", updated_at: "2013-10-31 02:36:27">, #<Activity id: 2, receiver_id: 2, creator_id: 1, link_id: 1, link_type: "Following", created_at: "2013-10-31 02:36:41", updated_at: "2013-10-31 02:36:41">, #<Activity id: 3, receiver_id: 2, creator_id: 1, link_id: 1, link_type: "Favorite", created_at: "2013-10-31 02:37:04", updated_at: "2013-10-31 02:37:04">]>

At the very least, this proves that Rails has the ability to do this. Circumventing default_scope seems like an issue with telling Rails what you want to do rather than a technical limitation.

UPDATE:

Turns out that when you pass a scope block to an association and call that association, self within that block refers to the relation. So, you can reflect on it and act appropriately:

class Activity < ActiveRecord::Base
  belongs_to :creator,  class_name: 'Artist', foreign_key: :creator_id,  inverse_of: :created_activities
  belongs_to :receiver, class_name: 'Artist', foreign_key: :receiver_id, inverse_of: :received_activities
  belongs_to :link, -> { self.reflections[:activity].active_record == Rating ? includes(:rating_explanation) : scoped }, polymorphic: true
end

I updated my code to reflect (haha) this.

This can be cleaned up. For example, maybe you don't always want to eager load rating_explanations when accessing activity links. There are a number of ways to solve that. I could post one if you'd like.

But, I think the most important thing that this shows is that within the association's scope block, you have access to the ActiveRecord::Relation being built. This will allow you to do things conditionally to it.

like image 71
Kaleidoscope Avatar answered Oct 29 '22 22:10

Kaleidoscope