gpt4 book ai didi

ruby-on-rails - 使用 Ransack 搜索值数组

转载 作者:数据小太阳 更新时间:2023-10-29 08:35:26 25 4
gpt4 key购买 nike

我是 Ransack 的新手,我遇到了一个似乎没有被 Ransack 完全涵盖的案例。我基本上是在尝试搜索一个值,但搜索到的值包含在一个数组中。

代码:

<%= 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 时,我得到了这个错误。

错误:

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.

Controller :

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 谓词进行搜索。不确定这是否是最好的方法,但它适用于我的情况。

关于ruby-on-rails - 使用 Ransack 搜索值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49370841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com