# Rails autocomplete

**URL:** <https://www.ruby-forum.com/t/rails-autocomplete/88491>\
**Category:** Rails\
**Created:** [March 12, 2007, 12:54am UTC](https://www.ruby-forum.com/t/rails-autocomplete/88491 "2007-03-12T00:54:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 12, 2007, 12:54am UTC](https://www.ruby-forum.com/t/rails-autocomplete/88491/1 "2007-03-12T00:54:49Z")

</div>

Hi,

A short question from newbie - hope wouldn’t be a problem for you.

I am playing with autocomplete features in rails -

In View:  
\<%= text\_field\_with\_auto\_complete :model, :attribute %\>

In Controller:  
auto\_complete\_for :model, :attribute  
(found many examples in www)

So far, so gut. Autocomplete works. But I want the following - if I type  
‘m’, only words starting with ‘m’ to appear.  
I have the word - ‘Belgium’ - when I fill ‘m’ in the field, ‘Belgium’ is  
fetched out, cause there is a ‘m’ in it.

Could someone explain me what is missing and probably give me some  
directions?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 12, 2007, 2:51am UTC](https://www.ruby-forum.com/t/rails-autocomplete/88491/2 "2007-03-12T02:51:24Z")

</div>

Any Dot wrote:

> So far, so gut. Autocomplete works. But I want the following - if I type  
> ‘m’, only words starting with ‘m’ to appear.  
> I have the word - ‘Belgium’ - when I fill ‘m’ in the field, ‘Belgium’ is  
> fetched out, cause there is a ‘m’ in it.

do this

“country LIKE ?%”

instead of this

“country LIKE %?%”

in your conditions clause.

Jamey

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 12, 2007, 3:09am UTC](https://www.ruby-forum.com/t/rails-autocomplete/88491/3 "2007-03-12T03:09:26Z")

</div>

Any Dot wrote:

> auto\_complete\_for :model, :attribute  
> (found many examples in www)
> 
> So far, so gut. Autocomplete works. But I want the following - if I type  
> ‘m’, only words starting with ‘m’ to appear.  
> I have the word - ‘Belgium’ - when I fill ‘m’ in the field, ‘Belgium’ is  
> fetched out, cause there is a ‘m’ in it.

Sorry, should have read your question closer.

This might work:

auto\_complete\_for :model, :country, :conditions =\> [ “LOWER(country)  
LIKE ?”, params[:country\_field].downcase + ‘%’ ]

If this doesn’t work, you might just have to define your own  
auto\_complete\_country method (see page 547 in Agile), which is really  
what auto\_complete\_for is doing for you anyway.

Jamey

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 12, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/rails-autocomplete/88491/4 "2007-03-12T15:23:49Z")

</div>

Thanks Jamey,

Actually, I wrote my own auto\_complete method - it is working now.

Many thanks
