我已经编写了正在运行的搜索功能,但我希望它不区分大小写 我怎么能让它变得麻木不仁,虽然我不想改变条件,因为它运行得很好。所有四个参数都是可选的,如果它们中的任何一个基于这种条件而存在。
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)
我使用PostgreSQL作为区分大小写的数据库。
答案 0 :(得分:5)
尝试使用LOWER
(sql)和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?