# Autocompete for first\_name OR last\_name

**URL:** <https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443>\
**Category:** Rails\
**Created:** [February 22, 2007, 10:27am UTC](https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443 "2007-02-22T10:27:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [February 22, 2007, 10:27am UTC](https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443/1 "2007-02-22T10:27:59Z")

</div>

My autocomplete method searches only on the last\_name column but I’d  
like for it to search first\_name and last\_name for a match. Basically  
how gmail and yahoo mail beta does it. I tried the sql OR operator but  
I can’t get it to work.

def autocomplete  
search = params[:user]  
@users = User.find(:all,  
:conditions =\> [ ‘LOWER(last\_name) LIKE ?’,  
‘%’ + search.downcase + ‘%’ ],  
:order =\> ‘last\_name ASC’,  
:limit =\> 8) unless search.blank?  
render :partial =\> “search”  
end

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [February 22, 2007, 10:46am UTC](https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443/2 "2007-02-22T10:46:03Z")

</div>

I don’t see anything wrong with:

@users = User.find(:all,  
:conditions =\> [ ‘LOWER(last\_name) LIKE ? OR LOWER(last\_name)  
LIKE ?’,  
“%{search.downcase}%”, “%{search.downcase}%” ],  
:order =\> ‘last\_name ASC’,  
:limit =\> 8)

Not able to test right now, mind you.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [February 22, 2007, 10:46am UTC](https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443/3 "2007-02-22T10:46:59Z")

</div>

whoops,  
that would be  
“%#{search.downcase}%”, “%#{search.downcase}%”  
sorry.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [February 22, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/autocompete-for-first-name-or-last-name/86443/4 "2007-02-22T12:49:17Z")

</div>

I see where I went wrong. Works perfect, thanks!
