Forum: Ruby on Rails find_by...

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.
slava (Guest)
on 2009-04-30 01:29
(Received via mailing list)
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.
Frederick C. (Guest)
on 2009-04-30 02:21
(Received via mailing list)
On Apr 29, 10:28 pm, slava <removed_email_address@domain.invalid> wrote:
>
You may find that the hash form of :conditions is the easiest way to
do this.

Fred
Neo (Guest)
on 2009-04-30 05:54
(Received via mailing list)
you can use to_param in the model witch is a screencast in
www.railscasts.com
2009/4/30 Frederick C. <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.
> >
>


--
明天会更好
This topic is locked and can not be replied to.