我用的是最新的mongoid...
我该如何做这个名为_scope的事件记录的mongoid等效项:
class Comment
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :post
field :body, :type => String
named_scope :recent, :limit => 100, :order => 'created_at DESC'
...
end
最佳答案
必须这样定义
scope :recent, order_by(:created_at => :desc).limit(100)
您可以查看范围的 mongoid 文档 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)
关于ruby-on-rails - 请按语法排序 Mongoid Scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6338752/