query a database table of Customers by first name, email, etc.
- git clone
https://github.com/ryanckulp/customer_autocomplete_demo.git
- bundle (might need to install ruby version via
rbenv install 2.6.1
first) rails db:create && rails db:migrate && rails db:seed
rails s
then visit localhost:3000 and try it
keyup
JS listener on the input field pings search via/customer_searches/new?query=X
- controller delegates search query to
Customer.search()
Customer.search()
performs case-insensitive search for "first_name" attribute only
can include email address, last name, etc attributes in search parameters via modifications like...
def self.search(params)
results = where("first_name ILIKE '%#{params[:query]}%'")
results += where("last_name ILIKE '%#{params[:query]}%'")
results += where("email ILIKE '%#{params[:query]}%'")
results.uniq.map { |c| { id: c.id, full_name: c.full_name } }
end