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?


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.


    Try this:

    @artist.activities.includes([{:link => :post},:creator,:receiver])...
    

    See the Rails docs for more.


    If I get this right, you not only want to load a polymorphic association's association , but a polymorphic association's polymorphic association .

    This basically means joining one defined table with another defined table through a bunch of undefined tables that come from some fields in the database.

    Since both the activity and the post are joined through a polymorphic object they both have a something_id and something_type column.

    Now I think active record doesn't let you do this out of the box but basically you want something like:

    class Activity
      has_one :post, :primary_key => [:link_id, :link_type], :foreign_key => [:postable_id, :postable_type]
    end
    

    (assuming your polymorphic association on Post is belongs_to :postable )

    That would then give you a query sort-of direct association between Post and Activity that is a very weird take on habtm with a 'polymorphic join table' (I just made that term up). Because both share the same polymorphicly associated object, they can be connected. Sort of like a friends-of-my-friends thing.

    CAVEAT : Like I said, as far as I know, AR doesn't let you do this out of the box (though it would be awesome) but there are some gems that give you composite foreign and/or primary keys. Maybe you can find one to help you solve your problem in this way.

    链接地址: http://www.djcxy.com/p/56934.html

    上一篇: 无法加载文件或程序集或其依赖项之一

    下一篇: 有没有办法来加载多态关联的关联?