Steve Polito Profile picture
I’m a Rails developer building https://t.co/z7D2QMozKp and sharing what I know on my blog https://t.co/OKZdCjvU0a and through my free course https://t.co/SAITGDOeuc
Mar 14, 2022 5 tweets 2 min read
Have you ever needed to get the most recent record for each user in #rails, but didn't know how to do it without using map?

Here's one way to do it with Active Record 👇 Calling group(:user_id).minimum(:id) returns a hash where the key is the user_id and the value is the ID of their latest (or first) post. Calling .values simply returns the IDs.

We can then pass that list to where(id:) to return a list of the latest (or first) posts per user.