所以我试图使用表上的SELECT Distinct选项Hashlog.select("DISTINCT tag").select("created_at").order("created_at DESC").limit(10)
1.9.3-p286 :017 > Hashlog.select("DISTINCT tag").select("created_at").order("created_at DESC").limit(10)
Hashlog Load (0.7ms) SELECT DISTINCT tag, created_at FROM "hashlogs" ORDER BY created_at DESC LIMIT 10
=> [#<Hashlog tag: "new", created_at: "2012-12-11 04:06:37">,
#<Hashlog tag: "now", created_at: "2012-12-11 04:06:33">,
#<Hashlog tag: "googleold", created_at: "2012-12-11 04:06:28">,
#<Hashlog tag: "google", created_at: "2012-12-11 04:06:26">,
#<Hashlog tag: "facebook", created_at: "2012-12-11 04:06:21">,
#<Hashlog tag: "facebook", created_at: "2012-12-11 04:06:18">,
#<Hashlog tag: "faceboot", created_at: "2012-12-11 04:06:15">]
因此,我希望结果只在标记列上是唯一的,但除非它通过select传递,否则它将不允许我按created进行排序。
最佳答案
我认为您要做的是为每个标记选择不同的标记值,这些值按最早的(即最小的)created_at
排序。这将是一种解决多个关联created_at
值的标记产生的歧义的方法。
如果是这样,请尝试以下操作:
Hashlog.select("tag, min(created_at) as earliest").group("tag").order("earliest DESC").limit(10)
.