From b257fef563f7830f0ae2897c87d57e2e9660f919 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 19 Sep 2016 20:10:11 -0400 Subject: [PATCH] Update customers_controller.rb --- app/controllers/customers_controller.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 3e6d26c..f3ec7bd 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -32,6 +32,19 @@ class CustomersController < ApplicationController default_search_scope :names + def auto_complete + @customers = [] + q = (params[:q] || params[:term]).to_s.strip + if q.present? + if q.match(/\A#?(\d+)\z/) + @customers << Customer.find_by_id($1.to_i) + end + @customers += Customer.where("LOWER(#{Customer.table_name}.name) LIKE LOWER(?)", "%#{q}%").order("#{Customer.table_name}.id DESC").limit(10).to_a + @customers.compact! + end + render :layout => false + end + # display a list of all customers def index if params[:search]