[英]Search an array of values with Ransack
我是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.
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
謂詞進行搜索。 不知道這是否是最好的方法,但是它適用於我的情況。
使用數組和Ransack並不是那么簡單,您必須手動完成許多工作,因為基礎查詢會迅速將您帶入高級領域。 檢查數組中的成員資格相對容易 ,根據LIKE模式檢查數組中的每個元素會更加復雜,因為您需要對unnest
函數調用進行LATERAL JOIN來解開數組,以便可以對它的成員進行LIKE:
select users.*
from users, unnest(users.rep_code_list) c -- This is an implicit LATERAL JOIN
where c ilike '%some-pattern%'
並且您可能想在SELECT子句中distinct on (users.id)
一個distinct on (users.id)
,以清除查詢其他部分出現的所有重復項:
select distinct on (users.id) users.*
from users, unnest(users.rep_code_list) c -- This is an implicit LATERAL JOIN
where c ilike '%some-pattern%'
and ...
為了使Ransack使用這樣的查詢,您需要添加一個范圍(並告訴Ransack它可以使用該范圍)或編寫一個自定義的ransacker 。 不幸的是,似乎沒有任何方法可以讓Ransack與通常的attr1_or_attr2_or...
參數名稱解析邏輯一起使用范圍,因此您的范圍必須全部完成:
class User < ApplicationRecord
def self.ransackable_scopes(auth = nil)
%i[things_like]
end
def self.things_like(s)
select('distinct on (users.id) users.*')
.joins(', unnest(users.rep_code_list) c')
.where('c ilike :pat or users.category ilike :pat or ...', pat: "%#{s}%")
end
end
然后以以下形式:
<%= f.search_field :things_like %>
您可能會更好地將LATERAL JOIN邏輯混搭到自定義ransacker中,或者更好的IMO中,用一個單獨的表替換該數組,以便可以使用Ransacker的關聯邏輯並將代碼視為數據庫中的一等實體,而不僅僅是字符串。 某種完全測試搜索(而不是Ransack)可能是另一種選擇。
您也許可以使用PostgreSQL的array_to_string
函數來對數組進行展平,但是您必須處理定界符,並且仍然會遇到“全部完成”作用域(或者可能是自定義ransacker)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.