Rails, working with named_scope

Rails, working with named_scope

I need to select some dynamic price ranges submitted from a search form. How should I approach this with scopes? I am looking for something like this

Painting.price_range(['1..500', '2000..5000'])

SELECT * FROM paintings WHERE price BETWEEN 1..500 **OR** BETWEEN 2000..5000 etc. 

Best regards. Asbjørn Morell.

Calculating the distance between two times


rails acts_as_state_machine problem
You'll need to use a lambda on the named_scope.

including Modules in controller
The following should work:.
How to select only specific attributes from a model?
  named_scope :price_range, lambda { |ranges|     {        :conditions => ["(" +           ranges.collect {"price between ? and ?"}.join(" or ") +                        ")"] +          ranges.collect {|r| [r.min, r.max]}.flatten      }   } 
The first ranges.collect creates as many "between ? and ?" checks as you have ranges and then the second ranges.collect flattens out the ranges and adds them as values to be sanitized into the conditions.

Rail cookies - Delete record from page after the date expiraton
I've stuck brackets round the ors just to be on the safe side..
Accessing post data in controller

Can i redirect_to in a new window

Running Rails Application on netbeans 6.7 error


named_scope :price_range, :conditions => ["(price BETWEEN 1 AND 500) OR (price BETWEEN 2000 AND 5000)"] 
named_scope :price_range, :conditions => ["(price ?) OR (price ?)", (1..500).to_s(:db), (2000..5000).to_s(:db)] 
named_scope :price_between, lambda { |from, to| { :conditions => ['price > ? AND price <= ?', from, to] } }  named_scope :price_between, lambda { |from, to| { :conditions => ['price BETWEEN ? AND ?', from, to] } } 

92 out of 100 based on 52 user ratings 302 reviews