Create a scope that groups unique id and recent date

14 views
Skip to first unread message

Bo Pritchard

unread,
Dec 4, 2013, 2:10:39 PM12/4/13
to rubyonra...@googlegroups.com
I want to write a scope that returns the most recent (created_at) row
for each person_id

So this table

id   person_id   created_at
1    111            2013-12-01 08:00
2    222            2013-11-20 07:00
3    111            2013-12-10 09:00
4    333            2013-10-21 08:00
5    333            2013-10-15 08:00

should return this

2    222            2013-11-20 07:00
3    111            2013-12-10 09:00
4    333            2013-10-21 08:00

Can someone help me out?
Many thanks
Reply all
Reply to author
Forward
0 new messages