Mike Haslam Mike Haslam - 9 days ago 6
Ruby Question

Ruby how to make search filters case insensitive

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

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

you can use lower and downcase to achieve that.