gpt4 book ai didi

ruby-on-rails - 如何根据 Mongoid 中的关系设置可链接范围

转载 作者:行者123 更新时间:2023-12-04 01:02:57 26 4
gpt4 key购买 nike

问题已解决...原来有一个被覆盖的事件记录方法,现在一切正常

我正在尝试设置范围,以便可以调用看起来像

Competitor.of_type(type).at_event(event)

这将返回参加事件的所有类型的竞争对手
我的模型看起来像
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

以下作品(返回mongoid标准)
Competitor.of_type(type)
Competitor.at_event(event)

但是当我链接它们时,它会打印出如下所示的内容:
#<Competitor:0x00000109e2b210>
#<Competitor:0x00000109e2ab08>
-------=-=------------------------------------
=> #<Mongoid::Criteria
selector: {},
options: {},
class: Competitor,
embedded: false>

每个 Competitor.of_type(type) (第一个链式条件)都有一个竞争者条目,如果我在查询上运行 .count,我会得到数据库中竞争者的总数。

在作用域的 mongoid 文档的顶部,它说所有作用域都是可链接的,也可以应用于关联,稍后将在关系部分讨论。
不幸的是,我没有看到关系子部分,我无法在主要关系部分中找到对范围的单一引用。

我能够得到以下结果来返回我想要的结果:
where(:id.in => event.competitor_ids).where(:type_id => type.id)

但如果查询的任何部分被拆分为单独的方法或范围,它就会失败并提供我上面显示的结果。

最佳答案

范围

Similar to Active Record, Mongoid allows you to define scopes on your models as a convenience for filtering result sets. Scopes are defined at the class level, either using the scope macro or by defining class methods that return a criteria object. All scopes are chainable and can be applied to associations as well, the later being discussed in the relations section.



命名作用域是在类级别使用作用域宏定义的,并且可以链接起来以在良好的 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)

Note that definitions are evaluated at class load time. For evaluation at runtime you will want to make sure to define using a proc or lambda. In the following example the first date is set as the date of class load, where the second scope sets the date at the time the scope is called.


scope :current, where(:start_date.lte => Date.today)
scope :current, -> { where(:start_date.lte => Date.today) }

类方法

对于那些喜欢 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)

返回标准的命名范围和类方法可以链接在一起——这就是 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)

关于ruby-on-rails - 如何根据 Mongoid 中的关系设置可链接范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8407704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com