转换对象的数组的ActiveRecord ::关系 [英] Converting an array of objects to ActiveRecord::Relation

查看:262
本文介绍了转换对象的数组的ActiveRecord ::关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的数组,让我们把它叫做指标。我想运行指标类方法在这个阵列上(这些的高清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)
结束
 

我想这凝结到两个问题。

  1. 如何转换的对象的数组到一个ActiveRecord ::关系? preferably没有做一个其中,各一次。
  2. 当运行一个高清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,这将给回一个关系其重新presents的相同的记录均会给你在数组中。

  • 有关ActiveRecord的4.x版,只需拨打<一href="http://api.rubyonrails.org/v4.1.8/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-all"><$c$c>all,它返回一个关系。

  

在运行高清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.

  1. How can I convert an Array of objects to an ActiveRecord::Relation? Preferably without doing a where each time.
  2. 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 call scoped, which will give back a Relation which represents the same records that all 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆