Skip to content

Instantly share code, notes, and snippets.

@jpignata
Created January 3, 2010 06:06
Show Gist options
  • Save jpignata/267847 to your computer and use it in GitHub Desktop.
Save jpignata/267847 to your computer and use it in GitHub Desktop.
class ActiveRecord::Base
class << self
def use_ilike_in_conditions_for_postgres(*conditions)
conditions = conditions.dup
@postgres_database ||= connection &&
connection.class.to_s.include?("PostgreSQLAdapter")
if @postgres_database && has_like_operator?(conditions)
conditions.each { |condition| replace_like_operator(condition) }
end
super_merge_conditions(*conditions)
end
private
def replace_like_operator(condition)
if condition.is_a?(Array)
condition.each { |c| replace_like_operator(c) }
elsif condition.is_a?(String)
condition.gsub!(/ like /i, " ilike ") if condition.is_a?(String)
end
end
def has_like_operator?(conditions)
conditions.flatten.join =~ / like /i
end
alias_method :super_merge_conditions, :merge_conditions
alias_method :merge_conditions, :use_ilike_in_conditions_for_postgres
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment