它必须这样定义
scope :recent, order_by(:created_at => :desc).limit(100)
您可以查看mongoid文档中的scopes here
从页面
命名作用域是在类级别使用作用域宏定义的,可以链接起来在一个好的DSL中创建结果集.
class Person
include Mongoid::Document
field :occupation, type: String
field :age, type: Integer
scope :rock_n_rolla, where(occupation: "Rockstar")
scope :washed_up, where(:age.gt => 30)
scope :over, ->(limit) { where(:age.gt => limit) }
end
# Find all the rockstars.
Person.rock_n_rolla
# Find all rockstars that should probably quit.
Person.washed_up.rock_n_rolla
# Find a criteria with Keith Richards in it.
Person.rock_n_rolla.over(60)