2015-11-28 25 views
1

我想我可能已經知道答案,但我希望可能有替代解決方案。在一個關聯中搜索字符串數組失敗與Ransack

我有一個User模型與has_many關聯到另一個模型Enrollment與字符串數組roles

我試圖運行例如角色的一個洗劫搜索:

:enrollments_roles_cont 'guest' 

不管我用哪個運營商的搜索失敗:

CONT:

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. 

EQ:

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. 我能以另一種方式解決這個問題嗎?

回答

1

它不漂亮,但它的工作原理:

在我Enrollments型號:

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