Forum: Ruby on Rails Best way to do filtering/refinements?

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.
E884d559ed311e48ec9831b895719b2a?d=identicon&s=25 Yanni Mac (yannimac)
on 2009-06-05 18:03
I have a Store model that has_many Products.  I need to allow the users
to filter on certain conditions in the products.  For example, products
at the store that cost more than $50.  I am planning on getting the
parameter in the controller and modifying the product collection.  I
have experimented with a few things, but I think there is probably an
elegant solution out there.  I want to be able to filter and then call
the collection using: store.products, since this is in a shared view.  I
have tried replacing the products with a new collection, but rails
automatically modifies the database (store.products =
filtered_products).  I dont want it to save as this is just going to be
a read.  Is there an easy way that I can pass conditions in store model
to get the products I want in the controller and then call the
collection using store.products in the view?
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 18:36
(Received via mailing list)
Do you mean there are products of different types ( models )
quite confused ??

On Fri, Jun 5, 2009 at 9:33 PM, Yanni Mac
<rails-mailing-list@andreas-s.net>wrote:

> filtered_products).  I dont want it to save as this is just going to be
> a read.  Is there an easy way that I can pass conditions in store model
> to get the products I want in the controller and then call the
> collection using store.products in the view?
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com
E884d559ed311e48ec9831b895719b2a?d=identicon&s=25 Yanni Mac (yannimac)
on 2009-06-05 18:52
There are not different models for products.  Product is one model and
it has fields for price, etc..  I want to filter out products based on
price:

:conditions=>"price>50"



Sandip Ransing wrote:
> Do you mean there are products of different types ( models )
> quite confused ??
>
> On Fri, Jun 5, 2009 at 9:33 PM, Yanni Mac
> <rails-mailing-list@andreas-s.net>wrote:
>
>> filtered_products).  I dont want it to save as this is just going to be
>> a read.  Is there an easy way that I can pass conditions in store model
>> to get the products I want in the controller and then call the
>> collection using store.products in the view?
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>> >
>>
>
>
> --
> Ruby on Rails Developer
> http://sandip.sosblog.com
> http://funonrails.wordpress.com
> www.joshsoftware.com
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-06-05 19:47
(Received via mailing list)
You're probably looking for named scopes. They can be used to add
conditions to an association, like store.products, on the fly.

For instance, if you have these named scopes on your Product model:

named_scope :price_less_than, lambda { |p| { :conditions => ['price
<= ?', p] } }
named_scope :price_more_than, lambda { |p| { :conditions => ['price
>= ?', p] } }

... and a seach form with two fields, :price_max and :price_min, then
this code will let you filter your results:

proxy = store.products
proxy = proxy.price_less_than(params[:price_max]) unless params
[:price_max].blank?
proxy = proxy.price_more_than(params[:price_min]) unless params
[:price_min].blank?

...then you can use proxy anywhere you would have used store.products
(feed it to will_paginate, etc). If you have very specific conditions
that would clutter up the model, you can also use .scoped to create an
anonymous scope.

The benefit here is that the actual records aren't retrieved until you
try to actually access elements of proxy, for instance by iterating
over it.

--Matt Jones

On Jun 5, 12:03 pm, Yanni Mac <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.