开发者

Heroku Search Broken - Rails 3.1

I've recently pushed a Rails 3.1 App to heroku. Locally, everything works fine, but in the live app, the search functionality is broken.

Model:

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

end

View:

<%= form_tag apps_path, :method => 'get', :id => "search" do %>
   <%= text_field_tag :search, params[:search] %>
   <%= submit_tag "Search", :name => nil, :class => "search-button" %>
<% end %>

Controller:

def index
    @开发者_如何学Pythonapps = App.search(params[:search])
    @number_of_apps = @apps.count
end

I have a feeling that it has to do with the fact that my local setup runs on SQLite3 and the Heroku setup uses PostgreSQL.

Any help is appreciated. :)


Please note that Postgres like matching is case-sensitive. Try using ILIKE instead of LIKE


There are two issues here:

  1. PostgreSQL LIKE keyword is different than LIKE in SQLite. LIKE in SQLite is ILIKE in PostgreSQL. Either use the right keyword or downcase the comparison.

  2. find(:all) has been deprecated since 2.3 and should have been removed in 3.1. Please use the #all method.

    def self.search(query)
      if query
        where('name LIKE ?', "%#{query}%").all
      else
        all
      end
    end
    

    Even better, return a scope to take advantage of lazy loading.

    def self.search(query)
      if query
        where('name LIKE ?', "%#{query}%")
      else
        self
      end
    end
    


You could use the Texticle gem. It's very easy to install and works wonderfully with Heroku, but you need to use PostgreSQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜