7

I'm trying to figure out the "best" place to put multi-attribute search form logic in a Rails application. The search form in question has several attributes which may or may not have values, and the data types differ between attributes. (For example, there are search options to search for items with a price attribute between two numbers, date ranges, string values, etc.) Also, the model in question has several nested attributes through has_a/has_many relationships and some of those attributes also need to be searchable.

The Rails mantra of thick model, thin controller makes me hesitant to try to aggregate the search logic into the controller. However, it also doesn't seem appropriate to put logic relating to constructing the search conditions in the model(s). Finally, in the spirit of DRY, I'm hesitant to hard-code a bunch of specific attribute names into some module since I will need to apply similar search logic to several unrelated models. Perhaps a naming convention of the form fields in the search view could be used to construct the right conditions? (Something like using prefixes like "min_", "max_", "startdate_" indicating the data type and search condition operator and the suffix being the name of the model and/or attribute?)

I've searched for advice on this, but most of the advice seems inflexible (hardcoded attribute names, no support for nested attributes) or to use route-based searches which I don't think will work for my need (where 5-10 parameters may be used in a search at once).

Any suggestions on the "Rails way" of doing this?

Chris Hart
  • 2,153
  • 1
  • 23
  • 45
  • 1
    Great question. Interesting Rails questions like this will rarely get much attention here on SO. But if they DO get noticed, you could get some great answers. Check out my question here http://stackoverflow.com/questions/1068558 which is actually related (somewhat). BTW, your idea about using attribute names is not bad, and seems to be a Railsy thing to do. – Dan Rosenstark Aug 28 '10 at 22:15
  • 1
    Yar, thanks for the link. Your commentary there is definitely useful to me - I was similarly contemplating where I would ultimately put the search logic once I figured out how to do it. After posting my question, I also came across a presentation on searching in Rails: http://www.scribd.com/doc/3188387/Advanced-Searching-in-Rails. I think the combination of the approaches laid out there, Dave's suggestion below, and your advice on organizing code that doesn't neatly fit the MVC paradigm will form the basis of my solution. Thanks! – Chris Hart Aug 29 '10 at 03:56
  • Sure thing, @Chris Hart, and good luck. – Dan Rosenstark Aug 30 '10 at 13:18

1 Answers1

1

If you are using 2.3.x, I've always found Searchlogic to be a solid and flexible starting point for searching ActiveRecord models where you don't need fulltext search. It supports associations, your own named scopes, etc.

If you are using Rails 3, the meta_search gem appears to offer similar functionality, but my experience with it is extremely limited.

Dave Pirotte
  • 3,806
  • 21
  • 14
  • 2
    I don't believe this was a question about which gem can I chuck in. It was a relevant question about how to organize your own code in a rails application. – npiv Nov 04 '11 at 09:58