# Best way to do filtering/refinements?

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590>\
**Category:** Rails\
**Created:** [June 5, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590 "2009-06-05T18:03:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 5, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590/1 "2009-06-05T18:03:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 5, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590/2 "2009-06-05T18:36:16Z")

</div>

Do you mean there are products of different types ( models )  
quite confused ??

On Fri, Jun 5, 2009 at 9:33 PM, Yanni M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](http://www.ruby-forum.com/).
> 
> >

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 5, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590/3 "2009-06-05T18:52:30Z")

</div>

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 R. wrote:

> Do you mean there are products of different types ( models )  
> quite confused ??
> 
> On Fri, Jun 5, 2009 at 9:33 PM, Yanni M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Ruby on Rails Developer  
> [http://sandip.sosblog.com](http://sandip.sosblog.com)  
> [http://funonrails.wordpress.com](http://funonrails.wordpress.com)  
> [www.joshsoftware.com](http://www.joshsoftware.com)

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [June 5, 2009, 7:47pm UTC](https://www.ruby-forum.com/t/best-way-to-do-filtering-refinements/169590/4 "2009-06-05T19:47:59Z")

</div>

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 J.

On Jun 5, 12:03 pm, Yanni M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
