17 Jul 2006
Dead Simple Full-text Searching with Rails
Using MySQL’s built-in FULLTEXT searching capabilities, you can add full-text searching to your Rails app’s pretty easily. It’s not quite as sexy as acts_as_searchable but implementation is pretty trivial and no dependencies are required. One caveat: your tables need to be MyISAM.
First, make sure you add a FULLTEXT index to your MySQL table like so:
ALTER TABLE articles ADD FULLTEXT (title, content);
Then, just use ActiveRecord’s find() method, but pass in a :conditions parameter:
@articles = Article.find(:all, :conditions => ['MATCH(title, content) AGAINST (?)', params[:query]])
