Rails 3 Active Record Initialize Search Chaining
I have a search function that performs basic filteri开发者_运维百科ng 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!
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
精彩评论