使用Ransack在关联中搜索字符串数组失败

和塔平

我想我可能已经知道答案了,但是我希望可以有替代的解决方案。

我有一个User模型与has_many另一个Enrollment具有字符串数组模型相关联roles

我试图在角色上运行Ransack搜索,例如:

:enrollments_roles_cont 'guest'

无论我使用哪个运算符搜索都会失败:

帐户:

PG::UndefinedFunction: ERROR:  operator does not exist: text[] ~~* unknown
LINE 1: ..." IS NULL AND ("enrollments_public_users"."roles" ILIKE '%ma...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

情商:

PG::InvalidTextRepresentation: ERROR:  malformed array literal: "manager"
LINE 1: ... IS NULL AND "enrollments_public_users"."roles" = 'manager' ...
                                                             ^
DETAIL:  Array value must start with "{" or dimension information.

我猜这失败了,因为我的“数组”列实际上是一个字符串。有两个问题。

  1. 将其切换为数组类型可以解决此问题吗?
  2. 我可以用其他方式解决吗?
和塔平

它不是很漂亮,但是可以工作:

在我的Enrollments模型中:

ransacker :roles do
  Arel.sql("array_to_string(roles, ',')")
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章