转换对象的数组的ActiveRecord ::关系 [英] Converting an array of objects to ActiveRecord::Relation
问题描述
我有对象的数组,让我们把它叫做指标
。我想运行指标类方法在这个阵列上(这些的高清self.subjects
的品种,范围等)。我所知道的一组对象运行类方法的唯一方法是让他们成为一个ActiveRecord ::关系。所以我最终诉诸添加 to_indicators
方法阵列
。
高清to_indicators
#TODO:让这个不太可怕。
Indicator.where ID:self.pluck(:ID)
结束
有时候我链相当多的这些范围的过滤下来的结果,在类的方法。所以,尽管我请一个ActiveRecord ::关系的方法,我不知道如何访问该对象。我只能得到它的内容通过所有
。但所有
是一个阵列。于是我不得不说数组转换成的ActiveRecord ::关系。例如,这是其中的一种方法的一部分:
all.to_indicators.applicable_for_bank(ID)。每个做|指示器|
共有+ = indicator.residual_risk_for(ID)
indicator_count + = 1,如果indicator.completed_by?(ID)
结束
我想这凝结到两个问题。
- 如何转换的对象的数组到一个ActiveRecord ::关系? preferably没有做一个
其中,
各一次。 - 当运行一个
高清self.subjects
在一个ActiveRecord ::关系类型的方法,我怎么访问的ActiveRecord ::关联对象本身?
感谢。如果我需要澄清什么,让我知道。
我如何转换对象的数组到一个ActiveRecord ::关系? preferably没有做,每个时间。
您可以不是一个数组转换为一个ActiveRecord ::关系,因为一个关系仅仅是一个建设者的SQL查询,其方法并不实际数据进行操作。
不过,如果你想要的是一个关系则:
-
有关的ActiveRecord 3.x中,不叫
所有
,而是叫<一href="http://api.rubyonrails.org/v3.2.21/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scoped"><$c$c>scoped$c$c>,这将给回一个关系其重新presents的相同的记录均
会给你在数组中。 -
有关ActiveRecord的4.x版,只需拨打<一href="http://api.rubyonrails.org/v4.1.8/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-all"><$c$c>all$c$c>,它返回一个关系。
在运行
高清self.subjects
在一个ActiveRecord ::关系类型的方法,我怎么访问的ActiveRecord ::关联对象本身?
在方法上调用关系对象,自
的关系(相对于它的定义的模型类)。
I have an array of objects, let's call it an Indicator
. I want to run Indicator class methods (those of the def self.subjects
variety, scopes, etc) on this array. The only way I know to run class methods on a group of objects is to have them be an ActiveRecord::Relation. So I end up resorting to adding a to_indicators
method to Array
.
def to_indicators
# TODO: Make this less terrible.
Indicator.where id: self.pluck(:id)
end
At times I chain quite a few of these scopes to filter down the results, within the class methods. So, even though I call a method on an ActiveRecord::Relation, I don't know how to access that object. I can only get to the contents of it through all
. But all
is an Array. So then I have to convert that array to a ActiveRecord::Relation. For example, this is part of one of the methods:
all.to_indicators.applicable_for_bank(id).each do |indicator|
total += indicator.residual_risk_for(id)
indicator_count += 1 if indicator.completed_by?(id)
end
I guess this condenses down to two questions.
- How can I convert an Array of objects to an ActiveRecord::Relation? Preferably without doing a
where
each time. - When running a
def self.subjects
type method on an ActiveRecord::Relation, how do I access that ActiveRecord::Relation object itself?
Thanks. If I need to clarify anything, let me know.
How can I convert an Array of objects to an ActiveRecord::Relation? Preferably without doing a where each time.
You cannot convert an Array to an ActiveRecord::Relation since a Relation is just a builder for a SQL query and its methods do not operate on actual data.
However, if what you want is a relation then:
for ActiveRecord 3.x, don’t call
all
and instead callscoped
, which will give back a Relation which represents the same records thatall
would give you in an Array.for ActiveRecord 4.x, simply call
all
, which returns a Relation.
When running a
def self.subjects
type method on an ActiveRecord::Relation, how do I access that ActiveRecord::Relation object itself?
When the method is called on a Relation object, self
is the relation (as opposed to the model class it’s defined in).
这篇关于转换对象的数组的ActiveRecord ::关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!