In Rails, Ransack search gives the error `PG::UndefinedFunction: ERROR: operator does not exist` when trying to filter by ID

In Rails, Ransack search gives the error `PG::UndefinedFunction: ERROR: operator does not exist` when trying to filter by ID

我正在尝试按 ID 过滤记录。这是我的搜索表单:

<%= search_form_for @search, url: dash_orders_path, id: 'searchForm' do |f| %>
    <div class="col-md-4 margin-top-15">
        <%= f.search_field :id_cont, class: 'form-control', placeholder: 'Order id' %>
    </div>
    <span class="button-sec pull-left width-wide center-text searchBtn">
        <%= f.submit "Search", class: "button-big blue" %>
        <%= f.submit "Clear", class: "button-big red", onclick: "$('#searchForm')[0].reset();" %>
    </span>
<% end %>

尝试进行搜索时,出现如下错误:

PG::UndefinedFunction: ERROR:  operator does not exist: integer ~~* integer
LINE 1: ...CT  "orders".* FROM "orders" WHERE ("orders"."id" ILIKE 0)  ...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT  "orders".* FROM "orders" WHERE ("orders"."id" ILIKE 0)  ORDER BY "orders"."updated_at" DESC LIMIT 2 OFFSET 0

可能是什么问题?

我遇到了类似的问题,发现您 post 正在尝试寻找解决方案。我是这样解决的:

问题是 PG 试图将一个整数(在我的例子中是一个日期)与一个字符串匹配。

我将谓词更改为 _eq,一切正常。

使用:field_cont(包含)谓词,Ransack在PG中的SQL查询是一个ILIKE,使用:field_eq是IS(=).

我想现在帮助你为时已晚,但也许其他人可以从这个答案中受益。

祝你好运,

让您继续将您的 ID 与 ransack 的 cont 谓词一起使用。将此添加到您的订单模型中:

ransacker :id do
  Arel.sql("to_char(\"#{table_name}\".\"id\", '99999')")
end

ransacker 方法允许您执行自定义搜索,在这种情况下,正如 this ransacker wiki 所说,ranasacker 方法可以避免输入的歧义,如果您使用 MySQL.

希望对您有所帮助。