I have user with name, location, created_at as important fields in table.
I want to retrieve for each user the latest location,i.e, I want something like this:
username location created_at
abc New York 2012-08-18 16:18:57
xyz Mexico city 2012-08-18 16:18:57
abc Atlanta 2012-08-11 16:18:57
only input is UId(1,2) array of userids.please help me to accomplish this.I just want to know how to write query using active record query interface.
Generally, this should be a standard way to solve this kind of problems:
The idea is to join the table with itself, and find those rows which don’t have any row with the same
userand greatercreated_at.Of course, you should have
(user, created_at)index on your table.Now you should see how would that be represented in AR interface.