Mongoid 5.0 - Rails 4 - Finder 停止工作

Mongoid 5.0 - Rails 4 - Finder stop working

知道发生了什么事吗?

我只是想找到第一个对象,例如

User.first

并且返回的总是一个随机对象,然后我尝试

User.last

结果是同一个对象

如果您使用的是 mongoid 5.0,则第一个和最后一个在没有明确定义排序的情况下不起作用

来自changelog

3768 first and last no longer add an _id sort when no sorting options have been provided. In order to guarantee that a document is the first or last, it needs to now contain an explicit sort.

这会起作用

User.asc(:id).first