title: Combining simple search, will_paginate, and ordering
body: <p>Hi am combining a simple search facility with will_paginate pagination, based on the respective and excellent RailsCasts tutorials.</p>
  
  <p>The code (in the model) is shown below,</p>
  
  <pre><code>def search(search, page)
  
    # See RailsCasts #37 Simple Search Form &amp; #51 will_paginate
  
    if search
      @matches = SalesActivity.where('salespeople.name LIKE ? OR products.name LIKE ?',
                                        "%#{search}%", "%#{search}%")
  
      @matches.paginate :per_page =&gt; 30,
                        :page =&gt; page,
                        :order =&gt; 'created_at DESC' # &lt;--- causes exception
    else
      paginate :include =&gt; [:salesperson, :product],
               :order =&gt; 'created_at DESC',  # works 
               :per_page =&gt; 30,
               :page =&gt; page
    end
  end
  </code></pre>
  
  <p>The ordering specified works fine in the paginate request on the path without the search.</p>
  
  <p>However, on the path that use the search result (@matches), I get the following error,</p>
  
  <pre><code>SQLite3::SQLException: ambiguous column name: created_at: SELECT ...  ORDER BY created_at DESC LIMIT 30 OFFSET 
  </code></pre>
  
  <p>If I remove the order parameter, it works fine.</p>
  
  <p>I'd appreciate suggestions for how to fix this.  Thanks.</p>