# Filtering results based on multiple criteria

**URL:** <https://www.ruby-forum.com/t/filtering-results-based-on-multiple-criteria/193076>\
**Category:** Rails\
**Created:** [August 10, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/filtering-results-based-on-multiple-criteria/193076 "2010-08-10T20:02:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardsugg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@richardsugg](https://www.ruby-forum.com/u/richardsugg)\
**Post date:** [August 10, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/filtering-results-based-on-multiple-criteria/193076/1 "2010-08-10T20:02:43Z")

</div>

What’s the paradigm for providing a set of filters for search  
results?

Here’s my example: I have a list of projects, and projects can be  
filtered by manager, status (open, closed, in progress), start date,  
etc. The filter settings are stored in the database using the  
preferences plugin, which works very nicely. However, my code is  
ugly, and I can’t detect when a filter has be unset, which makes me  
think there is a better way.

ProjectsController:

# the if statement always returns true even if show\_closed\_projects is

unchecked  
def index  
@projects = Projects.all  
if defined? params[:show\_closed\_projects] &&  
params[:show\_closed\_projects] == ‘on’  
current\_user.write\_preference(:show\_closed\_projects, true)  
@projects = @projects.closed # this is a named scope  
else  
current\_user.wrirte\_preference(:show\_closed\_projects, false)  
end  
end

views/projects/index.html.erb

# this works – if the preference is set, the checkbox is checked,

otherwise, it’s not checked.

 project filter options 

| \> Show closed projects? |
| --- |

\<%= submit\_tag "save filters" %\> \<%= link\_to "cancel" %\>

…projects listed here…
