title: Filtering ActiveRecord queries in rails
body: <p>I'm used to Django where you can run multiple filter methods on querysets, ie <code>Item.all.filter(foo="bar").filter(something="else")</code>.</p>
  
  <p>The however this is not so easy to do in Rails. <code>Item.find(:all, :conditions =&gt; ["foo = :foo", { :foo = bar }])</code> returns an array meaning this will not work: </p>
  
  <p><code>Item.find(:all, :conditions =&gt; ["foo = :foo", { :foo = 'bar' }]).find(:all, :conditions =&gt; ["something = :something", { :something = 'else' }])</code></p>
  
  <p>So I figured the best way to "stack" filters is to modify the conditions array and then run the query. </p>
  
  <p>So I came up with this function:</p>
  
  <pre><code> def combine(array1,array2)
     conditions = []
     conditions[0] = (array1[0]+" AND "+array2[0]).to_s
     conditions[1] = {}
     conditions[1].merge!(array1[1])
     conditions[1].merge!(array2[1])
     return conditions
   end
  </code></pre>
  
  <p>Usage:</p>
  
  <p>array1 = ["foo = :foo", { :foo = 'bar' }]
  array2 = ["something = :something", { :something = 'else' }]
  conditions = combine(array1,array2)
  items = Item.find(:all, :conditions => conditions)</p>
  
  <p>This has worked pretty well. However I want to be able to combine an arbitrary number of arrays, or basically shorthand for writing:</p>
  
  <pre><code>conditions = combine(combine(array1,array2),array3)
  </code></pre>
  
  <p>Can anyone help with this? Thanks in advance.</p>