# Searching multiple related models

**URL:** <https://www.ruby-forum.com/t/searching-multiple-related-models/131222>\
**Category:** Rails\
**Created:** [March 7, 2008, 12:46am UTC](https://www.ruby-forum.com/t/searching-multiple-related-models/131222 "2008-03-07T00:46:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dukael](https://www.ruby-forum.com/u/dukael)\
**Post date:** [March 7, 2008, 12:46am UTC](https://www.ruby-forum.com/t/searching-multiple-related-models/131222/1 "2008-03-07T00:46:32Z")

</div>

Hi,

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? ☹

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 F.
