简体   繁体   中英

How can I get my rails search bar in Production to search without case sensitivity

I have built a search bar into the Index page of my rails app without using the model. The search facility works terrific locally and returns all queries using the sqlite3' development database irrespective of whether I use upper or lower case text.

However after pushing the code into production on Heroku via (PG database), The search bar will ONLY bear results if the first letter in the search bar is UPPERCASE.

ie. Ford will generate search results of all of the Ford's.

BUT.. ford will do nothing.

What code can I add or modify in my controller to enable the search to proceed and bear results irrespective of case.

class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

def index
if params[:search].present? && !params[:search].nil?
  @pins = Pin.where("description LIKE ?", "%#{params[:search]}%").paginate(:page => params[:page], :per_page => 15)
else
  @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 15)
end
end

PostgreSQL (used on Heroku) is case sensitive by default. If you want to search with case insensitivity, use the ILIKE operator (instead of LIKE ) - http://www.postgresql.org/docs/9.4/static/functions-matching.html

The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.

But as Max said in the comments, the best thing to do is use the same database in development as production - this way, bugs like this are caught before they make it into production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM