Note that there are some explanatory texts on larger screens.

plurals
  1. POis there a way to eager load polymorphic association's associations?
    primarykey
    data
    text
    <p>artists have many activities (basically a cache of interactions between users):</p> <pre><code>class Activity &lt; ActiveRecord::Base belongs_to :receiver, :class_name =&gt; 'Artist', :foreign_key =&gt; :receiver_id #owns the stuff done "TO" him belongs_to :link, :polymorphic =&gt; true belongs_to :creator, :class_name =&gt; 'Artist', :foreign_key =&gt; :creator_id #person who initiated the activity end </code></pre> <p>For example: </p> <pre><code> Activity.create(:receiver_id =&gt; author_id, :creator_id =&gt; artist_id, :link_id =&gt; id, :link_type =&gt; 'ArtRating') </code></pre> <p>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.</p> <p>The controller looks like this:</p> <pre><code>class ActivityStreamController &lt; ApplicationController def index @activities = @artist.activities.includes([:link,:creator,:receiver]).order("id DESC").limit(30) end end </code></pre> <p>The db call correctly eagerly loads the polymorphic link objects:</p> <pre><code> 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) </code></pre> <p>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:</p> <pre><code>activity.link.post </code></pre> <p>It does a separate DB call for each art_rating's post. Is there a way to eagerly load the post as well?</p> <p><strong>UPDATE TO THE QUESTION:</strong></p> <p>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?</p> <p>I see in the DB log: </p> <pre><code>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) </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload