Created
January 3, 2010 06:06
-
-
Save jpignata/267847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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