Adam Perfect

General

Rails search in 5 minutes

I got to the point of needing to add search to the CMS I'm working on, so did a quick google search to see if there was any existing help for doing search in Rails. Up popped a result on the Rails Wiki, TextSearch. After reading through, it was a basic module for adding search code to the Rails models - nifty. Reading further down, the strengths of the wiki came to the fore as more people made suggestions on implementation and functionality, until at the bottom we get Nate McNamara's expanded version that supports a simple query language. I saved the code, added the two lines to my news post model and threw together a quick search controller. Within five minutes I've got a working news search, that can very easily be expanded to other areas (normal content, articles, etc.). With all this initial work basically done for me, I can now spend my time looking at things like aggregation to see if I can search across multiple fields and otherwise fine-tuning the code to my own needs. Rails and its community make life so much easier!

Written by Adam on

Adam is a Director of User Experience by day and photographer as time allows.

You may also like…