aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG3
-rwxr-xr-xactiverecord/lib/active_record/base.rb17
-rw-r--r--activerecord/test/finder_test.rb10
3 files changed, 28 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 3f729a42d4..720f203299 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,8 @@
*SVN*
+* Pass a range in :conditions to use the SQL BETWEEN operator. #6974 [dcmanges]
+ Student.find(:all, :conditions => { :grade => 9..12 })
+
* Fix the Oracle adapter for serialized attributes stored in CLOBs. Closes #6825 [mschoen, tdfowler]
* [DOCS] Apply more documentation for ActiveRecord Reflection. Closes #4055 [Robby Russell]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index a4c317ff3f..b6bdf65d0f 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -84,7 +84,7 @@ module ActiveRecord #:nodoc:
# Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
# The array form is to be used when the condition input is tainted and requires sanitization. The string form can
# be used for statements that don't involve tainted data. The hash form works much like the array form, except
- # only equality is possible. Examples:
+ # only equality and range is possible. Examples:
#
# class User < ActiveRecord::Base
# def self.authenticate_unsafely(user_name, password)
@@ -120,6 +120,9 @@ module ActiveRecord #:nodoc:
# Student.find(:all, :conditions => { :first_name => "Harvey", :status => 1 })
# Student.find(:all, :conditions => params[:student])
#
+ # A range may be used in the hash to use the SQL BETWEEN operator:
+ #
+ # Student.find(:all, :conditions => { :grade => 9..12 })
#
# == Overwriting default accessors
#
@@ -1264,6 +1267,7 @@ module ActiveRecord #:nodoc:
case argument
when nil then "IS ?"
when Array then "IN (?)"
+ when Range then "BETWEEN ? AND ?"
else "= ?"
end
end
@@ -1392,12 +1396,14 @@ module ActiveRecord #:nodoc:
# # => "name='foo''bar' and group_id= 4"
# { :status => nil, :group_id => [1,2,3] }
# # => "status IS NULL and group_id IN (1,2,3)"
+ # { :age => 13..18 }
+ # # => "age BETWEEN 13 AND 18"
def sanitize_sql_hash(attrs)
conditions = attrs.map do |attr, value|
"#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
end.join(' AND ')
- replace_bind_variables(conditions, attrs.values)
+ replace_bind_variables(conditions, expand_range_bind_variables(attrs.values))
end
# Accepts an array of conditions. The array has each value
@@ -1433,6 +1439,13 @@ module ActiveRecord #:nodoc:
end
end
+ def expand_range_bind_variables(bind_vars) #:nodoc:
+ bind_vars.each_with_index do |var, index|
+ bind_vars[index, 1] = [var.first, var.last] if var.is_a?(Range)
+ end
+ bind_vars
+ end
+
def quote_bound_value(value) #:nodoc:
if value.respond_to?(:map) && !value.is_a?(String)
if value.respond_to?(:empty?) && value.empty?
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index d31d859b44..1e6e4cf6cd 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -136,6 +136,16 @@ class FinderTest < Test::Unit::TestCase
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
end
+ def test_find_on_hash_conditions_with_range
+ assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
+ end
+
+ def test_find_on_hash_conditions_with_multiple_ranges
+ assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort
+ assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort
+ end
+
def test_find_on_multiple_hash_conditions
assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }