title: How can you conditionally build an ActiveRecord Query in Rails?
body: <p>I'm trying to build a search results page in my rails app (still a noob) and I can't figure out how to build a query the rails way.  </p>
  
  <p>For example, if no parameters are present I want to return all results.  If the user passes 1 to n optional parameters in the search form I want to add them to the query.  </p>
  
  <p>Next if they have a sort specified "price desc" or "year_built desc" or even a combination of both.</p>
  
  <p>Finally use will_paginate to separate the results</p>
  
  <pre><code># default to all listings
  @listings = Mls.all
  
  @listings.where("listing_price &gt; ?", params[:listing_price]) unless params[:listing_price].blank?
  # ... bunch of other search options ...
  @listings.where("property_type = ?", params[:property_type]) unless params[:property_type].blank?
  
  # order
  @listings.order("some order by param") if some sort param
  @listings.order("some order by param") if some other sort param
  
  # paginate results
  @listings.paginate(:page =&gt; params[:page])
  </code></pre>
  
  <p>Is there a "Rails" way of doing this?</p>