I have a location table in my database which contains location data of all the users of my system.
The table design is something like
id| user_id| longitude| latitude| created_at|
I have an array of users. Now I want to select the latest(sorted according to created at) location of all these users.
I am able to figure out the sql query for same
SELECT * FROM my_table
WHERE (user_id , created_at) IN (
SELECT user_id, MAX(created_at)
FROM my_table
GROUP BY user_id
)
AND user_id IN ('user1', 'user2', ... );
Now as I am working in Ruby On Rails, I want to write this sql query to activerecord rails. Can anyone please help me with this ?