title: Rails joins or preload belongs_to association from polymorphic model
body: <p>my problem is following. How can I joins belongs_to association from polymorphic model</p>
  
  <p>There is situation</p>
  
  <p>opinion.rb</p>
  
  <pre><code>class Opinion &lt; ActiveRecord::Base
      belongs_to :opinionable, :polymorphic =&gt; true
      belongs_to :category
  end
  </code></pre>
  
  <p>answer.rb</p>
  
  <pre><code>class Answer &lt; ActiveRecord::Base
      has_many :opinions, :as =&gt; :opinionable
  end
  </code></pre>
  
  <p>How can i do following</p>
  
  <blockquote>
    <p>Opinion.joins(:opinionabe).all</p>
  </blockquote>
  
  <p>it will throw</p>
  
  <blockquote>
    <p>ArgumentError: You can't create a polymorphic belongs_to join without specifying the polymorphic class!</p>
  </blockquote>
  
  <p>How can i specific which class i want to join?</p>
  
  <p>Second question. How to preload it?</p>
  
  <blockquote>
    <p>Opinion.preload(:opinionable).all</p>
  </blockquote>
  
  <p>works fine. It will do query for each class in belongs_to.</p>
  
  <p>But. if i want to do something like</p>
  
  <blockquote>
    <p>Opinion.preload(:opinionable => :answer_form).all</p>
  </blockquote>
  
  <p>there is problem because one model has this association and second hasn't. So it will throw exception.</p>
  
  <p>So how i can do something like</p>
  
  <blockquote>
    <p>Opinion.preload(:answer => :answer_form, :another_belongs_to_model).all</p>
  </blockquote>
  
  <p>?</p>
  
  <p>Thanks, David!</p>