1

I am building an app that has search fields how can I make search filters in my app case insensitive. Can I accomplish this in the following block of code:

<div class="filter">
  <%= form_tag posts_path, method: :get do %>
  <%= label_tag :bedrooms %>
  <%= text_field_tag :bedrooms, params[:bedrooms] %>

  <%= label_tag :bathrooms %>
  <%= text_field_tag :bathrooms, params[:bathrooms] %>

  <%= label_tag :neighborhood %>
  <%= text_field_tag :neighborhood, params[:neighborhood] %>

  <%= submit_tag "Search", name: nil %>
  <% end %>
</div> 

Here is relevant code from controller

 class PostsController < ApplicationController
   before_action :set_post, only: [:show, :edit, :update, :destroy]
   def home
   end  

 def index
   @posts = Post.all.paginate(:page => params[:page], :per_page => 15)
   @posts = @posts.where(bedrooms: params["bedrooms"])if
   params["bedrooms"].present?
   @posts = @posts.where(bathrooms: params["bathrooms"])if
   params["bathrooms"].present?
   @posts = @posts.where(neighborhood: params["neighborhood"])if
   params["neighborhood"].present?
 end
Mike Haslam
  • 245
  • 3
  • 21
  • This is not the relevant code. The relevant code is where you do the filtering itself, in a controller or model, comparing your filter params to the attributes of the objects in your model collections, or using them in database queries. – Amadan May 26 '15 at 06:12
  • Thanks to all for help as you can see I am a new to all this. I do understand downcase a little but not sure how to use in this context. it's only the neighborhood field I have trouble with. – Mike Haslam May 26 '15 at 06:33

3 Answers3

0
posts = Post.find(:conditions => ["lower(bedroom) = ?", params[:bedrooms].downcase])

you can use lower and downcase to achieve that.

Sachin R
  • 11,606
  • 10
  • 35
  • 40
0

Note: This writeup is based on the accepted answer to the question here.

If you are just coming in to the provided answers, here is a better explanation for using the lower function:

First of all, note: You could run into into incompatibilities - or worse: not realize some until damage is done, if you are using different Database for Development and different Database for Deployment. It's a bad idea™. Use the same RDBMS for development and production and save yourself the pointless trouble.


The lower() function works well both in SQlite and PostgreSQL and faster with its response.

  • In standard SQLite lower(X) only folds ASCII letters. For more, I quote the chapter Core Functions in the SQLite manual:

    The lower(X) function returns a copy of string X with all ASCII characters converted to lower case. The default built-in lower() function works for ASCII characters only. To do case conversions on non-ASCII characters, load the ICU extension.

    Better Emphasis here about PostgreSQL is that:

  • PostgreSQL lower(X) works with UTF-8 out of the box.


As a welcome side effect, you can speed up that query in PostgreSQL with an index on the expression lower(style), which will be faster than using ILIKE and a basic index on style.

Also, since PostgreSQL 9.1 you can use a GIN or GIST index with the pg_trgm extension to speed up any LIKE and ILIKE query - trigrams are case-insensitive. Detailed instructions and links in this related answer:

This writeup is adapted from answers in Stackoverflow and basically by Erwin Brandstetter

Community
  • 1
  • 1
Afolabi Olaoluwa
  • 1,898
  • 3
  • 16
  • 37
-1

you can use ilike in database Query.

Post.where("bedrooms ilike \"#{params[:bedrooms]}\" and bathrooms ilike \"#{params[:bathrooms]}\" and neighborhood ilike \"#{params[:neighborhood]}\"")
Kamesh
  • 1,435
  • 1
  • 14
  • 27