简体   繁体   English

Rails-Searchlogic将条件搜索为值数组

[英]Rails - Searchlogic to search condition as an array of value

I have two models Employee & Unit . 我有两个模型EmployeeUnit Unit has many Employees . 单位有很多员工 I am using SearchLogic to search employee model. 我正在使用SearchLogic搜索员工模型。 What is the equivalent of below SQL in Searchlogic 在Searchlogic中等效于以下SQL

employees.unit_id IN (1,2,3)

I have tried both 我都尝试过

unit_id_equals_all[] 
unit_id_equals_any[]

But nothing works. 但是什么都行不通。 Can anyone help? 有人可以帮忙吗?

Thanks, Abhilash 谢谢阿比拉什

Employee.unit_id_equals([1, 2, 3])

Same problem here. 这里同样的问题。

I have no idea why this worked for me, or why I even tried it, since it is undocumented. 我不知道为什么这对我有用,或者为什么我尝试过,因为它没有记录。 But I changed the _equals to _in, and it produced the SQL with IN, and worked perfectly. 但是我将_equals更改为_in,它使用IN生成了SQL,并且运行良好。

Employee.unit_id_in([1, 2, 3])

We had this same issue with a Rails 2.3.12 project. 我们在Rails 2.3.12项目中遇到了同样的问题。 With searchlogic 2.4.7 we could do: 使用searchlogic 2.4.7,我们可以做到:

User.id_equals([1,2,3])

After upgrading to search logic 2.5.8 (deprecation messages in the old one were cluttering out cucumber and spec output), this syntax no longer worked. 升级到搜索逻辑2.5.8(旧版本中的弃用消息已使黄瓜和规范输出杂乱无章)后,此语法不再起作用。 It threw this error just like the one above: 就像上面的错误一样,它引发了此错误:

ActiveRecord::StatementInvalid: Mysql::Error: Operand should contain 1 column(s): SELECT * FROM `users` WHERE (users.id = 1, 2, 3) 

After trying the solutions above, we found that these two syntax alternatives worked: 在尝试了上面的解决方案之后,我们发现这两种语法替代方法有效:

User.id_in([1,2,3]) <-- as suggested above
User.id_equals_any([1,2,3])

In other words, without the "_any" the new search logic produces invalid mysql. 换句话说,没有“ _any”,新的搜索逻辑将生成无效的mysql。 Looking into when and why this change might have occurred, I found this commit discussion: 调查何时以及为什么可能发生此更改,我发现了此提交讨论:

Github commit changing handling of arrays Github提交更改数组的处理

The upshot of this change and the discussion was to require _any for times when you want a match against any value in the array, and otherwise just pass the array in to the equals statement directly without changing the SQL to "IN" as needed for multiple values. 此更改和讨论的结果是,当您想与数组中的任何值进行匹配时,要求_any多次,否则只需将数组直接传递给equals语句,而无需将SQL更改为“ IN”即可值。

Reverting to 2.4.7 clears the error. 恢复到2.4.7可以清除错误。 Changing all the calls to the more explicit _any or _in is what we ended up doing in order to avoid deprecation errors. 为了避免弃用错误,我们最终将所有调用更改为更明确的_any或_in。 Hope this helps and adds to the very helpful answers above. 希望这会有所帮助并增加到上面非常有用的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM