简体   繁体   English

确定分组后如何选择记录。 (Rails 3.0)

[英]Determining how to pick a record after grouping. (Rails 3.0)

I have been using a scope that allows for the retrieval of the record with the highest id in a given group. 我一直在使用一个范围,该范围允许检索给定组中具有最高ID的记录。 I recently changed the way records are added to the database so that the record with the highest id is not necessarily the record with the most recent data; 我最近更改了将记录添加到数据库的方式,以使ID最高的记录不一定是最新数据的记录。 that would be the record with the highest :end_time. 那将是:end_time最高的记录。

scope :latest_record_in_group, unique.select('max(id) as id')

where unique is as follows: 其中唯一性如下:

scope :unique, group(:field_1, :field_2, :field_3, :field_4)

Now what I need is to select the id of the record with the highest :end_time and I'm baffled. 现在,我需要选择具有最高:end_time记录的ID,这让我感到困惑。 It seems that I can't alter how activeRecord decides which of the records in a group gets selected as the representative of said group. 看来我无法更改activeRecord如何决定选择组中的哪个记录作为该组的代表。 As it stands, it seems to be the record with the highest :id by default. 就目前而言,它似乎是默认情况下具有最高:id的记录。 Any insight as to how I can select the record with the highest :end_time would be greatly appreciated. 对于我如何选择具有最高:end_time最高记录的任何见解,将不胜感激。

EDIT: disambiguation 编辑:消歧

If I understand your question you should use 如果我了解您的问题,您应该使用

case you're using rails 3 如果您使用rails 3

Model.find(:all, :order => "end_time")

case you're using rails4 如果您使用的是rails4

Model.order(:end_time).all

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

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