MiG*_*GnH 12 scope ruby-on-rails mongoid
问题已解决......原来有一个活动的记录方法已经过了写,现在一切都按预期工作了
我正在尝试设置范围,以便我可以进行看起来像这样的调用
Competitor.of_type(type).at_event(event)
Run Code Online (Sandbox Code Playgroud)
这将返回参加活动的所有类型的竞争对手My Models看起来像
class Competitor < Competitor
belongs_to :type
has_and_belongs_to_many :events
scope :at_event, ->(event) {where(:event_ids.in => event.competitor_ids)}
scope :of_type, ->(type) where(:type_id => type.id)
end
Run Code Online (Sandbox Code Playgroud)
以下作品(返回mongoid标准)
Competitor.of_type(type)
Competitor.at_event(event)
Run Code Online (Sandbox Code Playgroud)
但当我链接它们时,它会打印出如下所示的内容:
#<Competitor:0x00000109e2b210>
#<Competitor:0x00000109e2ab08>
-------=-=------------------------------------
=> #<Mongoid::Criteria
selector: {},
options: {},
class: Competitor,
embedded: false>
Run Code Online (Sandbox Code Playgroud)
每个Competitor.of_type(类型)都有一个竞争者条目(第一个链接标准),如果我在查询上运行.count,我会得到数据库中竞争者的总数.
在范围的mongoid文档的顶部,它表示所有范围都是可链接的,并且也可以应用于关联,后面将在关系部分中讨论.不幸的是我没有看到关系子部分,我不能在主关系部分找到对范围的单一引用.
我能够得到以下内容以返回我想要的结果:
where(:id.in => event.competitor_ids).where(:type_id => type.id)
Run Code Online (Sandbox Code Playgroud)
但如果查询的任何部分被拆分为单独的方法或范围,它将失败并提供我上面显示的结果.
MZa*_*oza 10
领域
与Active Record类似,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)
Run Code Online (Sandbox Code Playgroud)
请注意,定义是在类加载时评估的.要在运行时进行评估,您需要确保使用proc或lambda进行定义.在以下示例中,第一个日期设置为类加载的日期,其中第二个范围设置调用范围时的日期.
scope :current, where(:start_date.lte => Date.today)
scope :current, -> { where(:start_date.lte => Date.today) }
Run Code Online (Sandbox Code Playgroud)
类方法
对于那些喜欢Data Mapper样式语法的人来说,返回条件的类方法也可以被视为可链接的范围.
class Person
include Mongoid::Document
field :occupation, type: String
field :age, type: Integer
class << self
def rock_n_rolla
where(occupation: "Rockstar")
end
def washed_up
where(:age.gt => 30)
end
def over(limit)
where(:age.gt => limit)
end
end
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)
Run Code Online (Sandbox Code Playgroud)
返回条件的命名范围和类方法可以链接在一起 - 这是Mongoid强大的标准API的美妙之处.
class Person
include Mongoid::Document
field :occupation, type: String
field :age, type: Integer
scope :washed_up, where(:age.gt => 30)
scope :over, ->(limit) { where(:age.gt => limit) }
def self.rock_n_rolla
where(occupation: "Rockstar")
end
end
# Same queries apply here as well.
Person.rock_n_rolla
Person.washed_up.rock_n_rolla
Person.rock_n_rolla.over(60)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5062 次 |
| 最近记录: |