title: Get a list of first record for each group
body: <p>I have a <code>Log</code> model with following columns:</p>
  <pre><code>[&quot;id&quot;, &quot;username&quot;, &quot;event&quot;, &quot;parameters&quot;, &quot;extras&quot;, &quot;created_at&quot;, &quot;updated_at&quot;]
  </code></pre>
  <p>I have indexes <code>created_at</code> and <code>username</code>.<br />
  Now, I would like to get the first log for each <code>username</code> ordered by <code>created_at</code>.</p>
  <p>One way to do this is to run the following query for each username:</p>
  <pre><code>log = Log.where(&quot;username = :username&quot;, username: username).order(:created_at).first
  </code></pre>
  <p>But this obviously queries the database a lot of times (equal to the number of usernames). Is there some way to do only one database query?</p>