Rails 3.2-#< Array:...的未定义方法`where'-查询Model.where() [英] Rails 3.2 - undefined method `where' for #<Array:... - Query of Model.where()

查看:94
本文介绍了Rails 3.2-#< Array:...的未定义方法`where'-查询Model.where()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将给定的 Header 中的字段与 Alarm 模型中的其他字段进行比较。正如您在代码中看到的那样,我通过3个不同的步骤过滤警报。前两个完美地工作。但是,最后一个不起作用。
它表示:

I am trying to compare fields from a given Header to other fields in the Alarm model. As you can see in the code I filter the alarms in 3 different steps. The first 2 work perfectly. However, the last one does not work. It said:

undefined method `where' for #<Array:...

据我了解,。其中是一种有效的类方法与数组。为什么在这里不工作?
我也尝试过使用 .find_all_by 和其他方法...但是没有用。

As far as I understand .where is a class method that works with array. Why does not work here? I also tried with .find_all_by and different things... but to not avail.

@header = Header.find(1)

# Extracts those alarms that are ACTIVE and have something in common with the tittles
@alarmsT = Alarm.activated.where("keyword in (?)", [@header.title_es, @header.title_en, @header.title_en])

# Extracts alarms when Header has at least the same categories as an alarm
@alarmsT = @alarmsT.select do |alarm| 
   @header.category_ids.all?{|c| alarm.category_ids.include? c }
end

// this is the one that does NOT work
# Extract alarms which share the same location as Header.events' town
@alarmsF = [] 
@header.events.each do |e|
    @alarmsF =  @alarmsF + @alarmsT.where("alarms.location LIKE ?", e.town)
end

任何帮助我发现我所缺少的东西,我们将不胜感激。谢谢

Any help spotting what I am missing much appreciated. Thanks

推荐答案

在第一行中,您成功返回了 ActiveRecordRelation @alarmsT

In your first line, you're successfully returning an ActiveRecordRelation object in @alarmsT

# Extracts those alarms that are ACTIVE and have something in common with the tittles
@alarmsT = Alarm.activated.where("keyword in (?)", [@header.title_es, @header.title_en, @header.title_en])

这时您可以应用其他 .where(...) @alarmsT 上的方法,条件或范围,以进一步构建ARel表达式并返回结果。

At this point you could apply additional .where(...) methods, conditions or scopes on @alarmsT to further build up the ARel expression and the results returned.

但是,然后对该关系运行过滤器,将 @alarmsT 转换为 Array

However, you then run a filter over this relation, converting @alarmsT to an instance of Array

# Extracts alarms when Header has at least the same categories as an alarm
@alarmsT = @alarmsT.select do |alarm| 
   @header.category_ids.all?{|c| alarm.category_ids.include? c }
end

由于 Array 不知道您的ARel的 .where(...)方法,或者您的任何警报模型的范围或属性。这就是为什么在下面的代码中您得到未定义的方法'where'的原因#< Array:... 错误-您正在调用 .where() Array 的实例上;

You can no longer build up the ARel expression, since Array doesn't know about your ARel's .where(...) method, or any of your Alarm model's scopes or attributes. This is why in the code below you're getting the undefined method 'where' for #<Array:... error -- you're calling .where() on an instance of Array; a method that does not exist.

@alarmsF = [] 
@header.events.each do |e|
  @alarmsF =  @alarmsF + @alarmsT.where("alarms.location LIKE ?", e.town)
end

您可以通过不执行select来按类别ID进行过滤,而使用联接来解决此问题。建立这样的联接(以验证相关表/列中至少有一个值的子集)在通过Google容易找到的地方以及此处的StackOverflow上都有大量记录。

You can fix this by not doing the select to filter by category ids and instead using a join. Building such a join (to verify existence of at least a subset of values in a related table/column) is documented quite a bit on places easily found through google and here on StackOverflow.

这篇关于Rails 3.2-#&lt; Array:...的未定义方法`where'-查询Model.where()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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