【问题标题】:Getting PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer获取 PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer
【发布时间】:2015-10-24 20:00:52
【问题描述】:

我正在尝试使用 ransack 进行简单搜索,但是当我执行搜索时出现以下错误:

ActiveRecord::StatementInvalid in GrupoFamiliars#index

PG::UndefinedFunction: ERROR:  operator does not exist: integer ~~* integer
LINE 1: ...rupo_familiars" WHERE ("grupo_familiars"."numero" ILIKE 0) L...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT  "grupo_familiars".* FROM "grupo_familiars" WHERE ("grupo_familiars"."numero" ILIKE 0) LIMIT 20 OFFSET 0
这是我的代码
def index
  @search = GrupoFamiliar.search(params[:q])
  @grupo_familiars = @search.result.paginate(:page => params[:page], :per_page => 20)
end

<%= search_form_for @search,url: grupo_familiars_path, :class => "form-inline", :builder => SimpleForm::FormBuilder do |f| %>
  <%= f.number_field :numero_cont, :class => "form-control", placeholder: "Numero" %>
  <button type="submit" class="btn btn-default">Buscar</button>
<% end %>

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails postgresql pg ransack


    【解决方案1】:

    尝试将其添加到您的模型中:

    class GrupoFamiliar
      # your code and stuff
      # ...
    
      private
    
      ransacker :numero do
        Arel.sql("to_char(\"#{table_name}\".\"numero\", '99999')")
      end
    end
    

    【讨论】:

    • 现在可以了!我不明白为什么,但谢谢。
    • 解释是:ransacker 不能正常使用非文本字段。这段代码 sn-p 基本上允许它在搜索查询中使用数字“numero”字段的文本表示(to_char 是 Postgres 的整数到字符串的转换函数),而不仅仅是原始的“numero”字段。不客气。
    • @Ezequiel 链接到 ransackers 文档 here 希望这可以帮助您一起使用 _cont 和 _eq!
    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2023-03-13
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多