title: How to get rails to do the correct single SQL join query for this iteration?
body: <p>I have basically no experience with Rails, and I'm trying to help a friend who is learning how to program, but I have a lot of experience with SQL, and I'm struggling to get Rails to do the right thing for a simple query with a join.</p>
  
  <p>I have a two tables, <code>drawings</code> and <code>drawing_types</code>.  <code>drawings</code> has a foreign key pointing into <code>drawing_types</code>, which has a <code>type_description</code> column.  This is obviously the most bare bones relation you can imagine.  In SQL, I'd do this to get all the drawings with their correct <code>type_descriptions</code>:</p>
  
  <pre><code>SELECT d.*, dt.type_description FROM drawings as d
  INNER JOIN drawing_types as dt ON dt.id = d.drawing_type_id;
  </code></pre>
  
  <p>This is a single efficient SQL query that returns exactly what I want: a list of drawings that also have their text type description.</p>
  
  <p>I cannot, for the life of me, get rails to generate and use a single query like this.  The best I can do is get it to do two queries, which clearly indicate it's doing the "join" in code, rather than letting the db do it.  Sometimes it likes to do N+1 queries!?!</p>
  
  <p>Here is the code for the model:</p>
  
  <pre><code>class Drawing &lt; ActiveRecord::Base
    attr_accessible :image, :drawing_type_id
    belongs_to :drawing_type
  end
  
  class DrawingType &lt; ActiveRecord::Base
    attr_accessible :type_description
    has_many :drawings
  end
  </code></pre>
  
  <p>Here is the relevant controller code:</p>
  
  <pre><code>@drawings = Drawing.includes(:drawing_type)
  </code></pre>
  
  <p>And here is the view:</p>
  
  <pre><code>&lt;% @drawings.each do |drawing| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;&lt;%= drawing.drawing_type.type_description %&gt;&lt;/b&gt;&lt;/td&gt;
  </code></pre>
  
  <p>This generates two SQL queries, one on <code>drawings.*</code>, and the other to get all the descriptions, which are then clearly being looked up in code.  If I change the <code>includes</code> to a <code>joins</code> it does 1+M+N SQL queries (M = number of drawing types, N = number of drawings)!?!?!?!</p>
  
  <p>I can get it to emit the correct SQL join to the log by using this (from <a href="https://stackoverflow.com/a/14357110/2119732">here</a>):</p>
  
  <pre><code>@drawings = Drawing.select("drawings.*, drawing_types.type_description").
       joins("INNER JOIN drawing_types ON drawing_types.id = drawings.drawing_type_id")
  </code></pre>
  
  <p>but, rails only seems to use the result as an array of drawings, ignoring the <code>type_description</code> column in the result, so it does an additional M+N queries!</p>
  
  <p>All of these variations "work" in the sense that the page renders correctly for all of them, but they are all the wrong way of doing this simple thing, relative to the single correct SQL join that should be executed. </p>
  
  <p>There simply has to be a simple way to do this correctly since it's the most basic of relations you might find in a totally boring website schema, but I have searched high and low, including reading lots of answers here and all the docs, and have not been able to figure out how to do this.</p>
  
  <p>Thanks,
  Chris</p>