title: ActiveRecord - querying polymorphic associations
body: <p>I am using polymorphic associations to track Comments in my project. All very straight forward stuff.</p>
  
  <p>The problem I have is in querying based on the polymorphic association and joining from the Comment model back to it's owner.</p>
  
  <p>So ... </p>
  
  <p>I have a Comment model</p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :commentable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>And a ForumTopics mode:</p>
  
  <pre><code>class ForumTopic &lt; ActiveRecord::Base
    has_many :comments, :as =&gt; :commentable
  end
  </code></pre>
  
  <p>I have several other "commentable" models that aren't important right now. 
  All of this works.</p>
  
  <p>What I am trying to do is find all of the Comments that belong to a ForumTopic with a specified condition (in this case, 'featured' == true).</p>
  
  <p>When I try and use a finder to join the models:</p>
  
  <pre><code>@comments = Comment.find(:all 
              :joins =&gt; :commentable
              :conditions =&gt; ["forum_topics.featured = ? ", true] 
              )
  </code></pre>
  
  <p>I receive the following error:</p>
  
  <blockquote>
    <p><em>Can not eagerly load the polymorphic association :commentable</em></p>
  </blockquote>
  
  <p>Using the AR "include syntax":</p>
  
  <pre><code>@comments = Comment.find(:all 
              :include =&gt; :forum_topics
              :conditions =&gt; ["forum_topics.featured = ? ", true] 
              )
  </code></pre>
  
  <p>returns:</p>
  
  <blockquote>
    <p><em>Association named 'forum_topics' was not found; perhaps you misspelled it?</em></p>
  </blockquote>
  
  <p>If I try and join with a table name instead of the association name (string instead of symbol):</p>
  
  <pre><code>@comments = Comment.find(:all,
              :joins =&gt; "forum_topics",
              :conditions =&gt; ["forum_topics.featured = ? ", true] 
              )
  </code></pre>
  
  <p>I see:</p>
  
  <blockquote>
    <p><em>Mysql::Error: Unknown table 'comments': SELECT comments.</em> FROM comments forum_topics WHERE (forum_topics.featured = 1 )*</p>
  </blockquote>
  
  <p>(You can see here that the syntax of the underlying query is totally off and the join is missing altogether).</p>
  
  <p>Not sure if what I am doing is even possible, and there are other ways to achieve the required result but it seems like it <em>should</em> be doable.</p>
  
  <p>Any ideas?
  Anything I am missing?</p>