One way is to put your legend in scope:
scope :budget_max, lambda { |max| where("budget < ?", max) unless max.nil? }
This will still become rather cumbersome since you are done:
Job.budget_min(params[:budget_min]).budget_max(params[:budget_max]) ...
A slightly different approach will use something like the following in your model: (based on the code here :
class << self def search(q) whitelisted_params = { :budget_max => "budget > ?", :budget_min => "budget < ?" } whitelisted_params.keys.inject(scoped) do |combined_scope, param| if q[param].nil? combined_scope else combined_scope.where(whitelisted_params[param], q[param]) end end end end
Then you can use this method as follows, and it should use whitelists if they are present in the parameters:
MyModel.search(params)
source share