title: Rails SQL Where If condition
body: <p>I am trying to create an advanced search form for a rails blog
  I'm trying to avoid using a gem
  I have a table <code>posts</code> with a <code>title</code>, <code>location_id</code> and <code>category_id</code>, the last two are connected to two tables Location (:small, :greater) and Category (name), but I doubt that's important for this question </p>
  
  <p>I have a search form which finds all posts where location_id and category_id is equal to the user set option </p>
  
  <pre><code>&lt;%= form_for(@advanced_search) do |f| %&gt;
   &lt;div class="field"&gt;
     &lt;%= f.label :category_search %&gt;&lt;br&gt;
     &lt;%= f.collection_select :category_search, Category.all, :id, :name, :
  include_blank =&gt; "any category" %&gt;
   &lt;/div&gt;
   &lt;div class="field"&gt;
     &lt;%= f.label :location_search %&gt;&lt;br&gt;
     &lt;%= f.collection_select :location_search, Location.all, :id, :locationsmallgreat
  ,:include_blank =&gt; "any city" %&gt;  
  
  &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Once again i doubt that's important for the question which is probably simpler than I'm explaining it to be </p>
  
  <p>Basically I have this so far </p>
  
  <pre><code>@posts = Post.all
           @posts = @posts.where('category_id = ?', @advanced_search.category_search) if @advanced_search.category_search != "any category"
           @posts = @posts.where('location_id = ?', @advanced_search.location_search) if @advanced_search.location_search != "any city"
  </code></pre>
  
  <p>But it doesn't seem to work 
  I need it to work as so, If the form has Category = Any category (:included_blank => "any category)
  and Location = London 
  It would search for all Posts where location is == london, BUT it would remove the .where for the category, seeing that it is == any city and vice versa if a Category was selected and Location was left blank (:included_blank => "any city)</p>
  
  <p>Thanks</p>