4

In my Rails app, I want to an incremental search (like autocomplete) through the Customer table in the DB, looking for names, and showing the results as you type, Google Instant-style.

My brief research for gems or examples that might help in this scenario didn't provide any useful results.
Is there a Rails way to implement this?


The only issue I can think of is that that having approx. 500k records in the DB (even if the name related columns are indexed) might slow down the responsiveness.
As a solution for that, the ajax request might be triggered only after typing in the first 4-5 characters.

Any other suggestions, leads, issues, ideas, comments?

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111

2 Answers2

8

Use this: Tokeninput

and here is Railscast for it: TokenInput railscast

bor1s
  • 4,081
  • 18
  • 25
1

While I'm not at all familiar with ruby, I use jQuery UI autocomplete often. You can trigger it to run only after a certain number of characters are typed (See minLength in 'options').

Also you can pass additional parameters to your server side scripting. I pass one called Limit and set it to a sensible value (10 in most cases), I use this in the SQL query (where rownum <10), that way you don't hammer the database.

Raoul
  • 3,849
  • 3
  • 24
  • 30