3

I have written search functionality which is working, but I want it to be case-insensitive.
How can I make it insensitive, though I don't want to change condition because this is running nicely. All four parameters are optional, if any of them are present based on this condition it works.

conditions = {}
conditions['first_name'] = params[:first_name] unless params[:first_name].blank?
conditions['last_name'] = params[:last_name] unless params[:last_name].blank?
conditions['specialities.name'] = params[:speciality] unless params[:speciality].blank?
conditions['locations.zipcode'] = params[:zipcode] unless params[:zipcode].blank?

search = Model.using_scope.where(conditions)

I am using PostgreSQL as database which is case-sensitive.

jvnill
  • 28,218
  • 4
  • 78
  • 83
Chitrank Samaiya
  • 780
  • 9
  • 20

1 Answers1

5

try using a combination of LOWER (sql) and downcase (ruby)

  search = Model.using_scope
  search = search.where('LOWER(first_name) = ?', params[:first_name].downcase) unless params[:first_name].blank?
  search = search.where('LOWER(last_name) = ?', params[:last_name].downcase) unless params[:last_name].blank?
  search = search.where('specialities.name = ?', params[:speciality]) unless params[:speciality].blank?
  search = search.where('locations.zipcode = ?', params[:zipcode]) unless params[:zipcode].blank?
jvnill
  • 28,218
  • 4
  • 78
  • 83