我在用最新的mongoid..。
如何执行此active record named_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
发布于 2011-06-14 12:32:43
它必须这样定义
scope :recent, order_by(:created_at => :desc).limit(100)
您可以查看范围here的mongoid文档
从页面
命名作用域是使用作用域宏在类级别定义的,并且可以链接起来以在一个很好的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)
https://stackoverflow.com/questions/6338752
复制相似问题