title: Fetch latest rows grouped by uniqe field value
body: <p>I have a table of Books with an <code>author_id</code> field.</p>
  
  <p>I'd like to fetch an array of Books which contain only one Book of every author. The one with the latest <code>updated_at</code> field.</p>
  
  <p>The problem with straightforward approach like <code>Books.all.group('author_id')</code> on Postgres is that it needs all requested field in its <code>GROUP BY</code> block. (See <a href="https://stackoverflow.com/a/6106195/1245302">https://stackoverflow.com/a/6106195/1245302</a>)</p>
  
  <p>But I need to get all Book objects one per author, the recent one, ignoring all other fields.
  It seems to me that there's enough data for the DBMS to find exactly the rows I want,
  at least I could do that myself without any other fields in <code>GROUP BY</code> block. :)</p>
  
  <p>Is there any simple Rails 3 + Postgres (version &lt; 9) or SQL implementation 
  independent way to get that?</p>
  
  <p><strong>UPDATE</strong>
  Nice solution for Postgres:</p>
  
  <pre><code>books.unscoped.select('DISTINCT ON(author_id) *').order('author_id').order('updated_at DESC') 
  </code></pre>
  
  <p>BUT! there still problem remains – results are sorted by <code>author_id</code> in the first place, but i need to sort by <code>updated_at</code> inside the same <code>author_id</code>-s (to find, say the top-10 recent book authors).</p>
  
  <p>And Postgres doesn't allow you to change order of <code>ORDER BY</code> arguments in <code>DISTINCT</code> queries :(</p>