2017-02-21 81 views
0

我試圖在Rails 5網站上實現高級搜索。用戶傳入參數「provider_type」,並且我想返回包含該值的所有記錄。該值是使用簡單形式從下拉列表中選擇的。我new.html.erb看起來是這樣的:在postgres列中搜索參數值

<%= simple_form_for Search.new, :html => {:class => 'form-horizontal' } do |f| %> 
    <%= f.input :provider_type, collection: ['Mental Health', 'Medical'] %> 
    <%= f.button :submit %> 
<% end %> 

我的搜索模式是這樣的:

class Search < ApplicationRecord 
    def search_providers 
     providers = Provider.all 
     providers = providers.where("provider_type LIKE ?", ['Mental Health', 'Medical']) if provider_type.present? 
     providers 
    end 
end 

我的搜索器:

def SearchesController < ApplicationController 
    def new 
     @types = Provider.uniq.pluck(:provider_type) 
    end 

    private 
     def search_params 
      params.require(:search).permit(:provider_type) 
     end 
    end 
end 

當我嘗試搜索「心理健康「的搜索形式,我得到這個錯誤:PG :: UndefinedFunction:錯誤:操作符不存在:字符變化[] ~~未知

EDIT

當我改寫它作爲

providers.where(provider_type: provider_type) if provider_type.present? 

這將產生錯誤「PG :: InvalidTextRepresentation:錯誤:畸形的數組文本: 「%心理健康%」 DETAIL:數組值MUT開始與 「{」或尺寸信息。

回答