我最近将我的应用程序上传到heroku,并且由于postgresql,区分大小写不起作用,我想知道如何解决这个问题?我的代码如下:
relation = Game.gamsearch(params[:gamsearch])
relation = Game.consearch(params[:consearch]) if params[:consearch].present?
relation = Game.gensearch(params[:gensearch]) if params[:gensearch].present?
relation = Game.where("game_name LIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
relation = Game.where("console = ?", params[:console]) if params[:console].present?
查看代码:
<%= form_tag games_path, :controller => 'games', :action => 'gamsearch', :method => 'get' do %>
<%= text_field_tag :gamsearch, params[:gamsearch] %>
<%= submit_tag t('.searchb'), :game_name => nil %>
<% end %>
这是型号代码:
def self.gensearch(*args)
#search for games by their genre
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << "( %s )" % str.split.map{|w| "genre LIKE ? "}.join(" OR ")
cond_values.concat(str.split.map{|w| "%#{w}%"})
end
all :conditions => [cond_text.join(" AND "), *cond_values]
结束
答案 0 :(得分:3)
relation = Game.where("game_name ILIKE ?", "#{params[:game_name]}%") if params[:game_name].present?
注意ILIKE而不是LIKE
答案 1 :(得分:2)
ILIKE是PG的方法,但如果您正在开发另一个数据库(sqlite),那可能无法正常工作。解决这个问题的一种方法是将两个值都设为大写,然后比较两者的大写版本。