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

发布于 2024-09-23 23:47:43 字数 337 浏览 0 评论 0原文

我有两个模型 Employee单位单位有许多员工。我正在使用 SearchLogic 来搜索员工模型。 Searchlogic 中以下 SQL 的等价物是什么

employees.unit_id IN (1,2,3)

我都尝试过

unit_id_equals_all[] 
unit_id_equals_any[]

但没有任何作用。有人可以帮忙吗?

谢谢,阿比拉什

I have two models Employee & Unit. Unit has many Employees. I am using SearchLogic to search employee model. What is the equivalent of below SQL in Searchlogic

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久随 2024-09-30 23:47:43
Employee.unit_id_equals([1, 2, 3])
Employee.unit_id_equals([1, 2, 3])
恰似旧人归 2024-09-30 23:47:43

这里同样的问题。

我不知道为什么这对我有用,或者为什么我什至尝试过它,因为它没有记录。但我将 _equals 更改为 _in,它生成了带有 IN 的 SQL,并且运行良好。

Employee.unit_id_in([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.

Employee.unit_id_in([1, 2, 3])
謌踐踏愛綪 2024-09-30 23:47:43

我们在 Rails 2.3.12 项目中也遇到了同样的问题。使用 searchlogic 2.4.7,我们可以这样做:

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

升级到 searchlogic 2.5.8 后(旧版本中的弃用消息使 cucumber 和规范输出变得混乱),这种语法不再有效。它抛出了这个错误,就像上面的错误一样:

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

在尝试了上面的解决方案之后,我们发现这两种语法替代方案有效:

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

换句话说,如果没有“_any”,新的搜索逻辑会产生无效的 mysql.txt 文件。研究何时以及为何发生此更改,我发现了此提交讨论:

Github 提交更改处理数组

这一更改和讨论的结果是,当您想要与数组中的任何值进行匹配时,需要 _any ,否则只需将数组直接传递到 equals 语句,而不将 SQL 更改为“ IN” 根据需要获得多个值。

恢复到 2.4.7 可清除错误。为了避免弃用错误,我们最终将所有调用更改为更明确的 _any 或 _in。希望这会有所帮助,并补充上面非常有用的答案。

We had this same issue with a Rails 2.3.12 project. With searchlogic 2.4.7 we could do:

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. 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. Looking into when and why this change might have occurred, I found this commit discussion:

Github commit changing handling of arrays

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.

Reverting to 2.4.7 clears the error. Changing all the calls to the more explicit _any or _in is what we ended up doing in order to avoid deprecation errors. Hope this helps and adds to the very helpful answers above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文