简体   繁体   English

指定用户的rails(2.3.5)最新记录

[英]rails (2.3.5) most recent record for given user

I have a rails model called UserScore which has user_id and computed_date. 我有一个名为UserScore的Rails模型,它具有user_id和computed_date。

It has records like 它有类似的记录

 user_id    computed_date
  1           2012-01-11
  1           2012-01-10
  2           2012-01-04
  3           2012-01-06
  1           2012-01-13

I want just the most recent (single) score for all users in the table. 我只想要表中所有用户的最新(单个)分数。 The dates for scoring are not all the same, so some users may not have been scored for weeks, others were scored yesterday.. 评分日期并不完全相同,因此某些用户可能没有数周获得评分,而其他用户则是昨天获得了评分。

I know I could just get them all and loop through, but this table has 20m records, looking for a way to leverage sql finder to narrow it down for me. 我知道我可以全部获取并循环遍历,但是此表有2000万条记录,正在寻找一种方法来利用sql finder为我缩小范围。

thanks Joel 谢谢乔尔

我没有方便使用的Rails 2.3,但是以下方法应该可以工作。

UserScore.find(:all, :select => "DISTINCT user_id, *", :order => "computed_date desc")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM