开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜