Searching multiple related models


I'm interested in some feedback about a solution for searching multiple related models by columns (as opposed to full-text). A brief example would be three models with typical has_many relations: Hotel (name, company) Room (number, type) Bed (type, age)

I have a search form that lists the fields for each model with a text input. Upon submission the search needs to find the results determined by the refinement criteria entered for each model, so Hotel refines Room which refines Bed. The results would be a table with each column dynamically generated based on the criteria entered.

My solution so far has been to produce a set of functions that recursively goes through the params nested hash starting from the base model (Hotel) and using the ActiveRecord association reflections to find out the column type so it can then string together appropriate conditions, essentially using 'LIKE' for text and '=' for everything else at the moment.

But working with heavily nested hashes and recursive functions is hard, and while it mostly works certain aspects are very difficult, like finding out the column names from the models and putting the columns in order. I use the human_attribute plugin to set nice column names in the model, I believe Rails had this but it was removed? :frowning:

So my questions are: is there a common way of doing this? Am I doing it the right way? Has someone extraced this kind of functionality into a plugin?

I hope this makes sense without the actual code, I can probably tidy it and put it up if need be.

Best regards, Andrew France