Group-wise Max and Relational Active Record

Whats the best what to perform a group-wise max in relational active record?

How to do a Group-wise Max in MySQL

Example: Users have many activities. You want to create a relationship ‘lastActivity’ the most recent activity.

i can easily create a relationship to accomplish the above but it will be very inefficient (either 1 query per user or if using with() will return all activities), so i’m looking to replicate the group-wise max