I have a set of users. They have many requests.
# user.rb
has_many :requests
now I'd like to sort them by the last request created_at
timestamp.
So if that a user has the most recent request (the one where created_at
is closest to the current time) they're #1 etc.
Any ideas?
I've tried
User.includes(:requests).order('requests.created_at')
however that sorts them by the first request created_at timestamp which is obviously not what I want. I wish to only retrieve their last request. Then sort the users last requests by their created at date and order the users by that.
Update: Also if the user doesn't have any requests, they should be at the bottom. Now they get placed on top if they don't have any.