title: Handling multiple filters (params) cleanly in controller
body: <p>I have a class called Post, and I need to be able to accommodate the following scenarios:</p>
  
  <ul>
  <li>If a user selects a category, only show posts from that category</li>
  <li>If a user selects a type, only show posts with that type</li>
  <li>If a user selects a category and type, only show posts from that category with that type</li>
  <li>If a user selects nothing, show all posts</li>
  </ul>
  
  <p>I'm wondering if it's inevitable that my controller is just going to look gross with a ton of conditionals... here's my flawed approach at tackling this – does anyone know how I can accomplish this?</p>
  
  <pre><code>class PostsController &lt; ApplicationController
  
    def index
      @user = current_user
  
      # If a user has not specified a type or category, 
      # show them everything
      @posts = Post.all
  
      # If a user has selected a category, but no type, only 
      # show posts from that category.
      if params[:category] &amp;&amp; !params[:type]
        category = Category.find(params[:category])
        @posts = @category.posts
      end
  
      # If a user has selected a category and a type, only show
      # posts from that category with that type
      if params[:category] &amp;&amp; params[:type]
        category = Category.find(params[:category])
        type = params[:type]
        @posts = category.posts.where(post_type: type)
      end
  
      # If a user has selected a type but not a category, show all 
      # of the posts with that type 
      if params[:type] &amp;&amp; !params[:category]
        type = params[:type]
        @posts = Post.where(post_type: post_type)
      end
    end
  
  end
  </code></pre>