ruby-on-railsruby-on-rails-3.2will-paginate

Displaying partial results when with will_paginate and Rails


I am using will_paginate for the first time. My initial implementation is working great. Something like:

@products = Product.paginate(:page => params[:page], :per_page => 10)

In the Product controller, this produces what I was expecting, and I can flip through product listings fine. What I'm trying to do now is show only products that contain the word "glass", and can't figure out how to get it done. Any ideas?


Solution

  • Use a where clause like this (assuming @product.name should contain "glass"):

    @products = Product.where('name LIKE ?', '%glass%').paginate(...)
    

    Don't forget the paginate arguments, which I removed for clarity.