Fix search in PG

This commit is contained in:
Raphael Sofaer 2011-06-22 15:07:05 -07:00
parent e8552fc417
commit a987b198fd
2 changed files with 22 additions and 13 deletions

View file

@ -45,13 +45,22 @@ class Person < ActiveRecord::Base
scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
def self.search_query_string(query)
where_clause = <<-SQL
profiles.first_name LIKE ? OR
profiles.last_name LIKE ? OR
people.diaspora_handle LIKE ? OR
profiles.first_name LIKE ? OR
profiles.last_name LIKE ?
SQL
if postgres?
where_clause = <<-SQL
profiles.first_name ILIKE ? OR
profiles.last_name ILIKE ? OR
people.diaspora_handle ILIKE ?
SQL
else
where_clause = <<-SQL
profiles.first_name LIKE ? OR
profiles.last_name LIKE ? OR
people.diaspora_handle LIKE ? OR
profiles.first_name LIKE ? OR
profiles.last_name LIKE ?
SQL
end
sql = ""
tokens = []
@ -62,7 +71,7 @@ class Person < ActiveRecord::Base
sql << " OR " unless i == 0
sql << where_clause
tokens.concat([token, token, token])
tokens.concat([up_token, up_token])
tokens.concat([up_token, up_token]) unless postgres?
end
[sql, tokens]
end

View file

@ -2,6 +2,11 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
# check what database you have
def postgres?
@using_postgres ||= defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
end
# Load the rails application
require File.expand_path('../application', __FILE__)
Haml::Template.options[:format] = :html5
@ -36,8 +41,3 @@ module Devise
end
end
end
# check what database you have
def postgres?
@using_postgres ||= defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
end