How do I use a named_scope to filter records in my model

Posted by kibyegon on Stack Overflow See other posts from Stack Overflow or by kibyegon
Published on 2010-06-17T09:09:31Z Indexed on 2010/06/17 9:13 UTC
Read the original article Hit count: 166

Filed under:
|

I have a model "Product" with a "description" field. Now I want to have a link in the index page that when clicked will show all products where the description is blank (empty).

In the model I have defined a named_scope like this

named_scope :no_description,      :conditions => { :description => "" }

I have checked that the named_scope works by calling Product.no_description.count on the console.

As far as I know, the controller is then supposed to handle the filter request from the link on the "index" action but be able to distinguish it from the default which is view all products.

 def index
    @products = Product.all
    ...

My problem is getting the controller handle the different request, what route to setup for the link on the view and the actual link on the view. Hope I explained my problem.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about named-scope