使用 Ransack 搜索值数组

2024-04-17

我是 Ransack 的新手,我遇到了 Ransack 未明确涵盖的案例。我基本上试图搜索一个值,但搜索到的值包含在一个数组中。

CODE:

<%= f.search_field :category_or_account_number_or_status_or_account_number_or_account_name_or_accounts_name_or_accounts_number_or_user_name_or_user_rep_code_list_cont_any %>

最后还有这一段user_rep_code_list_cont这是用户的默认数组属性,目前看起来像这样 ["al20", "b234"]

因此,当我在 Ransack 搜索栏中输入 al20 时,我收到此错误。

ERROR:

PG::UndefinedFunction: ERROR:  operator does not exist: character 
varying[] ~~* unknown
LINE 1: ..."name" ILIKE '%al20%') OR "users"."rep_code_list" ILIKE 
'%al...
                                                         ^
HINT:  No operator matches the given name and argument type(s). You 
might need to add explicit type casts.

控制器:

def index
  @q = Submission.submissions_for(user: current_user).ransack(params[:q])
  @submissions = @q.result.includes(:user, :accounts).ordered(current_user).page(params[:page]).per(25)
end

再说一遍,我不是 Ransack 专家,但这似乎是现在应该涵盖的内容。我想搜索数组模型上的属性。


我最终使用了自定义Ransacker对于这种情况:

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

这会将数组转换为字符串,以便 Ransack 可以使用cont谓词。不确定这是否是最好的方法,但它对我的情况有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Ransack 搜索值数组 的相关文章

随机推荐