使用 Ransack 在关联中搜索字符串数组失败
Searching a string array in an association fails with 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.
我猜这会失败,因为我的 'array' 列实际上是一个字符串。那么两个问题。
- 将其切换为数组类型会解决这个问题吗?
- 我可以用其他方法解决这个问题吗?
虽然不漂亮,但很管用:
在我的 Enrollments
模型中:
ransacker :roles do
Arel.sql("array_to_string(roles, ',')")
end
我想我可能已经知道答案了,但我希望可能有替代解决方案。
我有一个 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.
我猜这会失败,因为我的 'array' 列实际上是一个字符串。那么两个问题。
- 将其切换为数组类型会解决这个问题吗?
- 我可以用其他方法解决这个问题吗?
虽然不漂亮,但很管用:
在我的 Enrollments
模型中:
ransacker :roles do
Arel.sql("array_to_string(roles, ',')")
end