title: Rails: Structuring a query involving a polymorphic association and STI
body: <p>I'm trying to find the 10 most recent comments on photos so I can integrate them into an activity feed on my Rails 3.0.3 application.</p>
  
  <p>I've got a <strong>Photo</strong> model, which inherits from an <strong>Upload</strong> model using single table inheritance:</p>
  
  <pre><code>class Upload &lt; ActiveRecord::Base
      ...
  end
  
  class Photo &lt; Upload
      has_many :comments, :as =&gt; :commentable
      ...
  end
  </code></pre>
  
  <p>The polymorphic association <strong>commentable</strong> is described in the <strong>Comment</strong> model:</p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
      belongs_to :commentable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>So far so good, right?  The problem comes when I try to construct a query.  After some trial and error, I came up with this code that sits in the Photo model:</p>
  
  <pre><code>def self.latest_comments(count = 10)
      Comment.where(:commentable_type =&gt; "Upload")\
         .joins("INNER JOIN uploads ON comments.commentable_id = uploads.id")\
         .where("uploads.type" =&gt; "Photo").order("comments.created_at DESC").limit(count)
  end
  </code></pre>
  
  <p>This code works on my development machine with SQLite, but I've had to make some changes to get it to work on the production server using PostgreSQL.  The above incarnation of the query hasn't been tested on the production server yet, but I was looking for a cleaner way of structuring the query, as the above doesn't feel very robust, nor does it seem very 'Railsy'.</p>
  
  <p>What I'd like is to be able to say is</p>
  
  <pre><code>Comment.joins(:commentable =&gt; :photo)
  </code></pre>
  
  <p>...but this raises an exception, presumably because Rails doesn't know how to do the join:</p>
  
  <blockquote>
    <p>ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :commentable</p>
  </blockquote>
  
  <p>I came upon a <a href="https://stackoverflow.com/questions/680141/activerecord-querying-polymorphic-associations">post on StackOverflow</a> that described a different way of querying a polymorphic association.  I was able to come up with the following code based upon this post:</p>
  
  <pre><code>Comment.find_all_by_commentable_type("Upload", :include =&gt; :commentable,
          :order =&gt; "created_at DESC", :limit =&gt; 10)
  </code></pre>
  
  <p>However, this doesn't work for STI: since Photo is inheriting from Upload, I can't directly fetch the comments pertaining to photos - only the comments for all classes inheriting from Upload.  </p>
  
  <p>I've looked at a fair number of other posts relating to either polymorphism or STI, but none combine the two in the way I'm looking for.  I could just muddle around with the initial query, but does anyone have any thoughts on alternative ways to structure a query involving polymorphism <em>and</em> STI?</p>
  
  <p><strong>Edit</strong>: I found <a href="https://stackoverflow.com/questions/2603600/sti-and-polymorphs">another question</a> on here along the same lines as mine.  Unfortunately, the top answer didn't provide the solution I'm looking for.  If I were to apply it to my problem I'd be moving the <code>has_many :comments</code> association from the <code>Photo</code> model to the <code>Upload</code> model, and adding some code to the Comment class to determine the correct class of <code>commentable</code>.  This isn't ideal as it breaks the modelling in that any subclass of Upload will have comments.  There must be a better way...?</p>