模型里设置了默认作用域,如下所示:
class Article < ApplicationRecord
...
default_scope -> {order("created_at DESC")}
...
end
文章表设置了如下索引:
t.index ["category"], name: "index_articles_on_category"
t.index ["created_at"], name: "index_articles_on_created_at"
t.index ["user_id", "created_at"], name: "index_articles_on_user_id_and_created_at"
t.index ["user_id"], name: "index_articles_on_user_id"
为什么我使用
@articles = Article.all.paginate(page: params[:page], per_page: 10)
后按照时间排序查询不对,显示如下:
Rendering home/index.html.erb within layouts/application
(0.3ms) SELECT COUNT(*) FROM `articles`
Scoped order is ignored, it's forced to be batch order.
Article Load (0.3ms) SELECT `articles`.* FROM `articles` ORDER BY `articles`.`id` ASC LIMIT 10 OFFSET 0
CACHE (0.0ms) SELECT COUNT(*) FROM `articles`
搞不清为什么?难道是因为我用了 ruby2.5.0 和 rails 5.2.0rc1?