0

I'm attempting to implement an advanced search on my Rails 5 site. The user passes in a parameter "provider_type", and I would like to return all records that contain that value. The value is chosen from a dropdown list using simple-form. My new.html.erb looks like this:

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

My Search model looks like this:

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

And my Searches controller:

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

When I try to search for 'Mental Health' in the search form, I get this error: PG::UndefinedFunction: ERROR: operator does not exist: character varying[] ~~ unknown

EDIT

When I reword it as

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

This produces the error "PG::InvalidTextRepresentation: ERROR: malformed array literal: "%Mental Health%" DETAIL: Array value mut start with "{" or dimension information.

0

1 Answer 1

0

Probably you need not LIKE operator but IN. IN (or ANY) checks if fields match to one of element of array:

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

Rails 3 / Ruby: ActiveRecord Find method IN condition Array to Parameters single quote issue

Sign up to request clarification or add additional context in comments.

1 Comment

This produces the error "PG::InvalidTextRepresentation: ERROR: malformed array literal: "%Mental Health%" DETAIL: Array value mut start with "{" or dimension information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.