# Find\_by

**URL:** <https://www.ruby-forum.com/t/find-by/166827>\
**Category:** Rails\
**Created:** [April 29, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/find-by/166827 "2009-04-29T23:29:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 29, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/find-by/166827/1 "2009-04-29T23:29:21Z")

</div>

Hello,  
I am trying to construct a lookup based on params submited.  
possible urls…  
/gadgets?product\_id=1  
/gadgets?product\_id=1&product\_group\_id=2  
/gadgets?product\_location\_id=5  
/gadgets?product\_id=1&product\_location=5  
…

How do I write a universal lookup method ?

Gadget.find\_by\_product\_id  
Gadget.find\_by\_product\_id\_and\_product\_group\_id  
Gadget.find\_by\_product\_location\_id  
Gadget.find\_by\_product\_id&product\_location\_id

I want find\_by… to include only submitted params from a set of  
possible options.

Essentially I want find a way to avoid writing many if and else and  
wonder if anyone knows an easier and better approach.

---

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 30, 2009, 12:21am UTC](https://www.ruby-forum.com/t/find-by/166827/2 "2009-04-30T00:21:27Z")

</div>

On Apr 29, 10:28 pm, slava [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

You may find that the hash form of :conditions is the easiest way to  
do this.

Fred

---

<div class="post-metadata">

**Author:** ![slava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@slava](https://www.ruby-forum.com/u/slava)\
**Post date:** [April 30, 2009, 3:54am UTC](https://www.ruby-forum.com/t/find-by/166827/3 "2009-04-30T03:54:02Z")

</div>

you can use to\_param in the model witch is a screencast in

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com/)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

2009/4/30 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > /gadgets?product\_id=1&product\_location=5  
> > Gadget.find\_by\_product\_location\_id  
> > Gadget.find\_by\_product\_id&product\_location\_id
> > 
> > I want find\_by… to include only submitted params from a set of  
> > possible options.
> > 
> > Essentially I want find a way to avoid writing many if and else and  
> > wonder if anyone knows an easier and better approach.

–  
æ˜Žå¤©ä¼šæ›´å¥½
