# Find by multiple optional fields

**URL:** <https://www.ruby-forum.com/t/find-by-multiple-optional-fields/133355>\
**Category:** Rails\
**Created:** [March 30, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/find-by-multiple-optional-fields/133355 "2008-03-30T20:41:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@ptb](https://www.ruby-forum.com/u/ptb)\
**Post date:** [March 30, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/find-by-multiple-optional-fields/133355/1 "2008-03-30T20:41:37Z")

</div>

My application has several search fields. Each field is optional and  
if they are all left blank, I return some reasonable results. If the  
fields were simply conditional I could add each search term to a hash  
then pass that hash as conditions, like so:

conditions = {}  
conditions[:a] = 1 if params[:a] == 1  
conditions[:b] = 1 if params[:b] == 1

Task.find(:all, :conditions =\> conditions.empty? ? nil : conditions)

The problem is that some of the search terms aren’t that simple. One  
is a date field another is a name so I want to perform a find such as:

Task.find(:all, :conditions =\> [‘start \>= ?’, params[:date]])  
Task.find(:all, :conditions =\> [‘name LIKE ?’, “%#{params[:name]}%”])

…but I also want to include the conditions hash if there are simple  
comparison fields. I’d also like to potentially combine the two  
conditions above. How to creating a conditions array with multiple  
optional conditions isn’t obvious (to me at least). What I’m doing as  
a workaround is nesting the conditions has inside a with\_scope for  
each kind of “special” (not simple hash’able conditions) but it’s not  
DRY and it’s not clean.

Is there a way to use both a conditions hash and array with a find?  
Or is there a way to create a “\>=” or “LIKE” condition using a hash?

- Peter

---

<div class="post-metadata">

**Author:** ![ptb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@ptb](https://www.ruby-forum.com/u/ptb)\
**Post date:** [March 30, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/find-by-multiple-optional-fields/133355/2 "2008-03-30T21:43:18Z")

</div>

ptb wrote:

> The problem is that some of the search terms aren’t that simple. One  
> is a date field another is a name so I want to perform a find such as:
> 
> Task.find(:all, :conditions =\> [‘start \>= ?’, params[:date]])  
> Task.find(:all, :conditions =\> [‘name LIKE ?’, “%#{params[:name]}%”])

There are a few model plugins which can help with creating queries. One  
that I use is squirrel:

> **[thoughtbot case studies featuring design and development projects for...](https://thoughtbot.com/case-studies)**
>
> Check out some thoughtbot case studies featuring design and development projects for websites, mobile and web applications across HealthTech, EdTech, FinTech B2B and B2C.

which I find works extremely well.
