views:

17

answers:

1

I have a search function that performs basic filtering in a Rails 3 application (using the new method chaining). The filtering uses optional parameters and looks something like this:

class User < ActiveRecord::Base

  def self.search(params = {})
    users = User.?

    users = users.where(:sin => params[:sin]) if params[:sin]
    ...
    users = users.where("name LIKE :q", :q => "%params[:q]%") if params[:q]
  end

end

I am unsure how to setup a default users to include all users. I'd like the search functionality to return all records if no params are specified, and otherwise filter. Any ideas? Thanks!

+1  A: 
class User < ActiveRecord::Base

  def self.search(params = {})
    users = User.scoped

    users = users.where(:sin => params[:sin]) if params[:sin]
    ...
    users = users.where("name LIKE :q", :q => "%params[:q]%") if params[:q]
  end

end
Simone Carletti
Thanks Simone! Exactly what I was looking for!
Kevin Sylvestre