# Creating search using active record

**URL:** <https://www.ruby-forum.com/t/creating-search-using-active-record/162566>\
**Category:** Rails\
**Created:** [March 11, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/creating-search-using-active-record/162566 "2009-03-11T13:23:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [March 11, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/creating-search-using-active-record/162566/1 "2009-03-11T13:23:37Z")

</div>

I am trying to create a search with two fields but struggling on how to  
query it in active record.

I have a view with two fields. text field #userstring and search by  
value #searchby

eg search bob by eithier name or id value

How do i format a query using active record to read in the parameters  
and do the search condition on eithier name or id?

Ive looked at some examples but cant seem to find one that uses this  
type of search.

Any examples would great

thanks

Nick

---

<div class="post-metadata">

**Author:** ![nickh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ea924/32.png) [@nickh](https://www.ruby-forum.com/u/nickh)\
**Post date:** [March 11, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/creating-search-using-active-record/162566/2 "2009-03-11T13:32:26Z")

</div>

Nick H. wrote:

> Ive looked at some examples but cant seem to find one that uses this  
> type of search.
> 
> Any examples would great
> 
> thanks
> 
> Nick

if you have two fields (in first you enter name, id in second), just  
create condition to which field has been filled and then perform search,  
just like

# suppose _name_ column exists in table _users_

@user = []  
@user = User.find\_by\_name(params[:userstring]) unless  
params[:userstring]  
@user = User.find(params[:searchby]) unless params[:searchby]

if you have one field and would like to perform search through two  
columns, use

User.find(:first, :conditions =\> [userstring=? or id=?', params[:field],  
params[:field])

## tom

===============================================================================  
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz
