title: Rails 3 autocomplete with JQuery - need to clean up my controller code
body: <p>My autocomplet works, and it looks decent enough, but oh boy, the controller is a mess.  The autocomplete searches across multiple models which does not help things, and totalling up the found sets ect - there are country flags for customers etc.</p>
  
  <p>I'd like to extract this to a partial ideally or another way to make the code easier to deal with.</p>
  
  <pre><code>  def autocomplete
        customer_count = (Customer.select("company").where("LOWER(company) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", true).count) + (Product.select("model_number,description").where("LOWER(description) LIKE ? OR LOWER(model_number) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", "#{params[:term].downcase}%", true).count) + (Order.where("LOWER(order_number) LIKE ? AND deleted != true","#{params[:term].downcase}%").count)
        @customers = [{:label =&gt; (customer_count &gt; 1 ? "&lt;div style='font-weight: bold; text-align: left;'&gt;Show all #{customer_count} records&lt;/div" : customer_count &gt; 0 ? '' : "&lt;div style='font-weight: bold; text-align: left;'&gt;No records found&lt;/div&gt;"), :link =&gt; "/customers?company=#{params[:term].downcase}"}]
        @customers.concat(Customer.select("id, company, country").where("LOWER(company) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", true).limit(24).collect{|customer|{:label=&gt;("&lt;div style='float:left;margin-top:4px;margin-right:8px;'&gt;&lt;img alt='Mini_usa' src='/images/#{customer.country_div}'/&gt;&lt;/div&gt; #{customer.company}").html_safe,:link =&gt;"/customers/#{customer.id}"}})
        @customers.concat(Product.select("id, model_number, description").where("LOWER(description) LIKE ? OR LOWER(model_number) LIKE ? AND deleted != ?","#{params[:term].downcase}%","#{params[:term].downcase}%",true).limit(24).collect{|product|{:label =&gt; ("#{product.model_number.titlecase}&amp;nbsp;#{product.description.titlecase}").html_safe,:link =&gt;"/products/#{product.model_number}"}})
        @customers.concat(Order.select("id, order_number").where("LOWER(order_number) LIKE ? AND deleted != true","#{params[:term].downcase}%").limit(24).collect{ |order| { :label =&gt; ("#{order.order_number}").html_safe, :link =&gt;"/orders/#{order.id}" } })
    end
  </code></pre>