Question

I just launched my rails app with heroku (http://fast-reaches-9399.herokuapp.com/). The search works on my local development environment, but not in the production environment. Could this be because of a problem with postgres vs. sqlite?

I did this:

group :development do
  gem 'sqlite3', '1.3.5'
end

group :production do
  gem 'pg', '0.12.2'
end

just like in this tutorial (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-heroku_setup).

Here is my relevant code for searching. I used this (http://railscasts.com/episodes/37-simple-search-form) for help.

/search.html.erb

<h2>Results:</h2>

<ul>
    <% @colleges.each do |college| %>
        <li><%= link_to "#{college.name}", "#{college.url}" %></li>
    <% end %>
</ul>

application.html.erb (my layout file)

<%= form_tag("/search", :method => 'get') do -%>
    <li id="search"> <%= search_field_tag :search, params[:search] %></li>
<% end -%>

static_pages_controller.rb

def search
  @colleges = College.search(params[:search])
end

college.rb

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

Solution

As declan said, this is caused by the fact that postgres is case sensitive when using LIKE. The problem with using ILIKE is that it won't work in sqlite anymore.

A kind of hack which works is to change the search condition to:

find(:all, :conditions => ['UPPER(name) LIKE ?', "%#{search.upcase}%"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top