What is the Rails 3 way to order .group() results in Activerecord (here by "created_at")?
@messages = Message.group(:foo)
only results in displaying the oldest message. I'd need the latest to be shown.
I tried
@messages = Message.group(:foo).having("created_at = MAX(created_at)")
with no success. Any hints appreciated!
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
Update: trying the SQL way, this was supposed to work:
@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")
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