Forum: Ruby on Rails Creating search using active record

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7af028825f6c0f24554c69b2c93729a0?d=identicon&s=25 Nick Hoyle (hoylen)
on 2009-03-11 13:23
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
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-03-11 13:32
(Received via mailing list)
Nick Hoyle 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  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
This topic is locked and can not be replied to.