title: Rails 3: .group() ordered by created_at
body: <p>What is the Rails 3 way to order .group() results in Activerecord (here by "created_at")?</p>
  
  <pre><code>@messages = Message.group(:foo)
  </code></pre>
  
  <p>only results in displaying the oldest message. I'd need the latest to be shown.</p>
  
  <p>I tried </p>
  
  <pre><code>@messages = Message.group(:foo).having("created_at = MAX(created_at)")
  </code></pre>
  
  <p>with no success. Any hints appreciated!</p>
  
  <p>To clarify: I'm looking to have the group ordered within itself, not a normal messages.order("...").
  Should there be no easy Activerecord syntax, i'd be happy with raw SQL as well</p>
  
  <hr>
  
  <p>Update: trying the SQL way, this was supposed to work:</p>
  
  <pre><code>@messages = Message.find_by_sql("
    SELECT messages.* 
    FROM messages 
    GROUP BY messages.foo 
    HAVING messages.created_at = MAX(messages.created_at) 
    ORDER BY messages.created_at DESC")
  </code></pre>
  
  <p>But this retrieves solely single records (those that are not grouped). Supposedly grouped ones are being omitted. Do not know why, all records have :created_at and :foo values</p>