prolog12345
prolog12345

Reputation: 137

search in ruby on rails (change search string to lowercase)

I have a simple search feature, but when i search for the name, I need to enter the name exactly as it appears including upper and lower case. for example if i search for 'ruby' and the name is 'Ruby' it won't show the result

view source code:

<div class="row center">
    <%= form_tag pages_home_path, :method => 'get' do %>
    <%= text_field_tag :search, params[:search], :placeholder => "Search", :style => "text-align: center; font-size: 20px" %>
    <button type="submit_tag" name="action" class="waves-effect waves-light btn">Search Tutorials</button>
<%end%>

controller source code:

def index
@tutorials = Tutorial.search(params[:search])
end

model source code:

def self.search(search)
if search
  where(["name LIKE ?","%#{search}%"])
else
  all
end

how can i do this so that when the user types in the name to search for, it shows the result, even if it upper or lower case?

Upvotes: 2

Views: 1195

Answers (2)

quazar
quazar

Reputation: 590

Use ILIKE in place of LIKE. It will make like query not case sensitive.

Upvotes: 3

Rustam Gasanov
Rustam Gasanov

Reputation: 15791

How about this?

where(["LOWER(name) LIKE ?","%#{search.downcase}%"])

Upvotes: 4

Related Questions