Sql Rails邮件日期排序问题

Sql Rails邮件日期排序问题,sql,ruby-on-rails,sorting,activerecord,Sql,Ruby On Rails,Sorting,Activerecord,我在使用ActiveRecord按日期排序帖子时遇到问题。。。这不是我所期望的 1.9.3p194 :019 > Post.all(:select => [:id, :updated_at], :order => "updated_at DESC") Post Load (0.7ms) SELECT id, updated_at FROM "posts" ORDER BY published_at DESC, updated_at DESC +----+---------

我在使用ActiveRecord按日期排序帖子时遇到问题。。。这不是我所期望的

1.9.3p194 :019 > Post.all(:select => [:id, :updated_at], :order => "updated_at DESC")
  Post Load (0.7ms)  SELECT id, updated_at FROM "posts" ORDER BY published_at DESC, updated_at DESC
+----+---------------------------+
| id | updated_at                |
+----+---------------------------+
| 22 | 2012-08-16 18:59:28 -0600 |
| 9  | 2012-08-16 18:58:16 -0600 |
| 11 | 2012-08-15 20:18:53 -0600 |
| 1  | 2012-08-15 20:18:52 -0600 |
| 12 | 2012-08-15 20:18:53 -0600 |
| 6  | 2012-08-15 20:18:52 -0600 |
| 13 | 2012-08-15 20:18:53 -0600 |
| 2  | 2012-08-15 20:18:52 -0600 |
| 15 | 2012-08-15 20:18:53 -0600 |
| 5  | 2012-08-16 21:49:14 -0600 |
| 17 | 2012-08-15 20:18:53 -0600 |
| 4  | 2012-08-15 20:18:52 -0600 |
| 20 | 2012-08-15 20:18:53 -0600 |
| 7  | 2012-08-15 20:18:52 -0600 |
| 21 | 2012-08-15 20:18:53 -0600 |
| 14 | 2012-08-15 20:18:53 -0600 |
| 10 | 2012-08-15 20:18:53 -0600 |
| 8  | 2012-08-15 20:18:53 -0600 |
| 19 | 2012-08-15 20:18:53 -0600 |
| 18 | 2012-08-15 20:18:53 -0600 |
| 16 | 2012-08-15 20:18:53 -0600 |
| 3  | 2012-08-15 20:18:52 -0600 |
+----+---------------------------+
请注意,post#5在8月16日进行了更新,但它不会出现在顶部!请注意,Rails似乎对其进行了正确排序

1.9.3p194 :020 > Post.all(:select => [:id, :updated_at], :order => "updated_at DESC").sort_by &:updated_at
  Post Load (0.6ms)  SELECT id, updated_at FROM "posts" ORDER BY published_at DESC, updated_at DESC
+----+---------------------------+
| id | updated_at                |
+----+---------------------------+
| 1  | 2012-08-15 20:18:52 -0600 |
| 2  | 2012-08-15 20:18:52 -0600 |
| 3  | 2012-08-15 20:18:52 -0600 |
| 4  | 2012-08-15 20:18:52 -0600 |
| 6  | 2012-08-15 20:18:52 -0600 |
| 7  | 2012-08-15 20:18:52 -0600 |
| 8  | 2012-08-15 20:18:53 -0600 |
| 10 | 2012-08-15 20:18:53 -0600 |
| 11 | 2012-08-15 20:18:53 -0600 |
| 12 | 2012-08-15 20:18:53 -0600 |
| 13 | 2012-08-15 20:18:53 -0600 |
| 14 | 2012-08-15 20:18:53 -0600 |
| 15 | 2012-08-15 20:18:53 -0600 |
| 16 | 2012-08-15 20:18:53 -0600 |
| 17 | 2012-08-15 20:18:53 -0600 |
| 18 | 2012-08-15 20:18:53 -0600 |
| 19 | 2012-08-15 20:18:53 -0600 |
| 20 | 2012-08-15 20:18:53 -0600 |
| 21 | 2012-08-15 20:18:53 -0600 |
| 9  | 2012-08-16 18:58:16 -0600 |
| 22 | 2012-08-16 18:59:28 -0600 |
| 5  | 2012-08-16 21:49:14 -0600 |
+----+---------------------------+
我相信这很简单。。。如果您能提供任何帮助,我们将不胜感激

我试过这些都没有用:

Post.all(:order => "updated_at DESC")
Post.all(:order => "updated_at DESC").limit(1)

我想我已经弄明白了。。。。我在“created_at”上有一个默认的作用域,我认为它把我搞砸了。

好吧,SQL肯定是不正确的

SELECT id, updated_at FROM "posts" ORDER BY published_at DESC, updated_at DESC
应该是

SELECT id, updated_at FROM "posts" ORDER BY updated_at DESC
编辑:

是的,默认范围意味着它将按发布时间和更新时间排序。如果要保留模型中的默认范围,但不使用此查询的范围,可以使用:

Post.unscoped.all(:select => [:id, :updated_at], :order => "updated_at DESC")

你是如何得到这些漂亮的打印表格的?Hirb-控制台中输出结果的宝石。