Created
July 7, 2010 01:20
-
-
Save endel/466168 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
require 'ar-extensions' | |
# ActiveRecord::Extensions fix for using ILIKE with PostgreSQL database | |
# | |
# Usage sample: | |
# User.find(:all, :conditions { :name_ilike => "%something%" }) | |
module ActiveRecord::Extensions | |
class ILike | |
def self.process( key, val, caller ) | |
values = [*val] | |
if key.to_s =~ /(.+)_ilike$/ | |
str = values.collect do |v| | |
"#{caller.quoted_table_name}.#{caller.connection.quote_column_name( $1 )} ILIKE " + | |
"#{caller.connection.quote( '%%' + v + '%%', caller.columns_hash[ $1 ] )} " | |
end | |
else | |
return nil | |
end | |
str = str.join(' OR ') | |
result_values = [] | |
str.gsub!(/'((%%)?([^\?]*\?[^%]*|[^%]*%[^%]*)(%%)?)'/) do |match| | |
result_values << $2 | |
'?' | |
end | |
result_values = nil if result_values.empty? | |
return Result.new(str , result_values) | |
end | |
end | |
register ILike, :adapters => [ :postgresql ] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment