2

I am fairly new to rails, and am using this search form

  <%= form_tag producer_path(@user), method: :get do %>
    <p>
      <%= text_field_tag :search, params[:search] %>
      <%= submit_tag "Search", name: nil %>
    </p>
  <% end %>

and in the model a SQL LIKE

  def self.search(search)
    if search
      find(:all, conditions: ['name LIKE ?', "%#{search}%"])
    else
      find(:all)
    end
  end

I expected this to be case insensitive by default, but it isn't. I am using PostgreSQL 9.1. Is this something I need to configure for the database, and if so where?

BTW I see the following paramaters in the URL including an UTF8 hash with an unprintable character (in the browser address bar, here in html it seems to show up as a check sign), which I don't know where that came for. Could this UTF8 encoding be the source of the problem?

?utf8=✓&search=

2 Answers2

1
find(:all, conditions: ['name iLIKE ?', "%#{search}%"])

Quoting from postgres docs:

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.

manoj
  • 1,615
  • 15
  • 19
0

Here is a similar thread,

How can I search (case-insensitive) in a column using LIKE wildcard?

which is not really a rails solution. I would suggest you to try meta-search gem for searching which have sophisticated features.

Community
  • 1
  • 1
maximus ツ
  • 7,369
  • 3
  • 22
  • 51