Skip to content

Instantly share code, notes, and snippets.

@joshnabbott
Created April 10, 2009 16:48
Show Gist options
  • Select an option

  • Save joshnabbott/93164 to your computer and use it in GitHub Desktop.

Select an option

Save joshnabbott/93164 to your computer and use it in GitHub Desktop.
module ActiveRecord
module Searchable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def searchable_on(*args)
args.flatten!
@args = args.compact
self.instance_eval do
def search(query)
fields = @args.join(',')
query = ActiveRecord::Base.connection.quote(query)
all({
:conditions => "MATCH (#{fields}) AGAINST (#{query} IN BOOLEAN MODE)",
:order => 'score DESC',
:select => "SQL_CALC_FOUND_ROWS *, MATCH (#{fields}) AGAINST (#{query}) AS score"
})
end
end
end
end
end
end
ActiveRecord::Base.class_eval { include ActiveRecord::Searchable }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment