How do I select all records grouped by a column sorted by created_at desc.
I might want to retrieve all the latest comments for each Articles. No matter what I do the group(:article_id), will always return the oldest comment.
Best regards. Asbjørn Morell
Something like :
Comment.order('created_at DESC').all
That should do it :)
If you just want the first result, use first
instead of all
.
You also can use limit
. Exemple to get the firsts 5 results :
Comment.order('created_at DESC').limit(5).all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With