Skip to content

Instantly share code, notes, and snippets.

@endel
Created July 7, 2010 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endel/466168 to your computer and use it in GitHub Desktop.
Save endel/466168 to your computer and use it in GitHub Desktop.
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