title: Return records distinct on one column but order by another column
body: <p>I am building a Rails 3 app with a pretty standard message model. I would like to return the most recently created message records for each unique conversation_id. It seems like a fairly simple task, but I have not been able to code or find a working solution.<br>
  Admittedly, I am not super SQL savvy either (as I have gotten by with mainly Active Record queries thus far). Here is what I'm trying to accomplish.</p>
  
  <p>Sample messages table:</p>
  
  <pre><code>| id | sender_id | receiver_id | conversation_id | subject | body | created_at |
  | 1  |     *     |      *      |        1        |    *    |   *  |    16:01   |
  | 2  |     *     |      *      |        2        |    *    |   *  |    17:03   |
  | 3  |     *     |      *      |        1        |    *    |   *  |    18:04   |
  | 4  |     *     |      *      |        3        |    *    |   *  |    19:06   |
  | 5  |     *     |      *      |        2        |    *    |   *  |    20:07   |
  | 6  |     *     |      *      |        1        |    *    |   *  |    21:08   |
  | 7  |     *     |      *      |        4        |    *    |   *  |    22:09   |
  </code></pre>
  
  <p>The return I would like to get only the most "recent" message record for each <code>conversation_id</code> and ordered by <code>created_at DESC</code>:</p>
  
  <pre><code>| id | sender_id | receiver_id | conversation_id | subject | body | created_at |
  | 7  |     *     |      *      |        4        |    *    |   *  |    22:09   |
  | 6  |     *     |      *      |        1        |    *    |   *  |    21:08   |
  | 5  |     *     |      *      |        2        |    *    |   *  |    20:07   |
  | 4  |     *     |      *      |        3        |    *    |   *  |    19:06   |
  </code></pre>
  
  <p>My original solution in SQLite worked just fine:  <code>GROUP BY (conversation_id)</code>. However, apparently that solution is unique to SQLite and does not work with Postgres.</p>
  
  <p>Next, I tried: <code>SELECT DISTINCT ON (conversation_id) *</code>. However, this also requires <code>ORDER BY (conversation_id)</code> which I do not want. I want to order by <code>created_at</code>.</p>