简体   繁体   English

在Rails的上下文中,如何在第二秒之前对列进行排序?

[英]In the context of rails, how can I order columns by the second?

I am working with rails and postgresql. 我正在使用Rails和Postgresql。 I have a created_at timestamp on a model. 我在模型上有一个created_at时间戳。 I am in a situation where I need to order the results by second. 我处于需要按秒排序结果的情况。 I can't seem to get rails todo this. 我似乎无法做到这一点。

eg If I insert 5 records in 1 minute, and then want to sort by created_at time, since they all have the same minute, it starts to order them by alpha because they all have the same insert minute. 例如,如果我在一分钟内插入5条记录,然后想按created_at时间排序,因为它们都具有相同的分钟数,因为它们都具有相同的插入分钟数,所以它开始按alpha排序。

How can I make it so that it orders by the insert in seconds. 我如何才能使其在几秒钟内按插入顺序排序。 Postgres is storing it. Postgres正在存储它。 But DateTime doesn't seem to take seconds into account in this way... 但是DateTime似乎并没有以这种方式考虑几秒钟...

I hope this makes sense 我希望这是有道理的

Kirk 柯克

ActiveRecord order method should sort including the second ActiveRecord order方法应排序,包括第二个

Eg: 例如:

Note the last two datetimes are in the same minute 请注意,最后两个日期时间在同一分钟内

User.pluck(:created_at)
=> [Fri, 14 Mar 2014 16:26:02 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 18:33:51 UTC +00:00]

User.order("created_at desc").collect(&:created_at)
 => [Fri, 14 Mar 2014 18:33:51 UTC +00:00, Fri, 14 Mar 2014 18:33:42 UTC +00:00, Fri, 14 Mar 2014 16:26:02 UTC +00:00]

Adding: 新增:

default_scope -> { order('created_at DESC') } default_scope-> {order('created_at DESC')}

to your ActiveRecord should do it because created_at should look like this:"2014-03-12 15:29:26" which specifies the seconds. 您的ActiveRecord应该执行此操作,因为created_at应该如下所示:“ 2014-03-12 15:29:26”,它指定秒数。

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

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