aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-05-26 00:35:49 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-05-26 00:35:49 +0000
commit2be3a33f32b43287b692be2cc2550f159b00165e (patch)
tree4b5b55de4af68f8bc2b7d23b73687ba33d83bc77
parent9dac6b759f57d93247ba6e8b8d8c7b9d5638dc22 (diff)
downloadrails-2be3a33f32b43287b692be2cc2550f159b00165e.tar.gz
rails-2be3a33f32b43287b692be2cc2550f159b00165e.tar.bz2
rails-2be3a33f32b43287b692be2cc2550f159b00165e.zip
find gracefully copes with blank :conditions. Closes #7599.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb6
-rw-r--r--activerecord/test/finder_test.rb13
3 files changed, 18 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 5a2318ef1e..9bf09e97c1 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* find gracefully copes with blank :conditions. #7599 [Dan Manges, johnnyb]
+
* validates_numericality_of takes :greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to, :odd, and :even options. #3952 [Bob Silva, Dan Kubb, Josh Peek]
* MySQL: create_database takes :charset and :collation options. Charset defaults to utf8. #8448 [matt]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 81b492e65e..3ba1cb26d1 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1249,11 +1249,11 @@ module ActiveRecord #:nodoc:
def add_conditions!(sql, conditions, scope = :auto)
scope = scope(:find) if :auto == scope
segments = []
- segments << sanitize_sql(scope[:conditions]) if scope && scope[:conditions]
- segments << sanitize_sql(conditions) unless conditions.nil?
+ segments << sanitize_sql(scope[:conditions]) if scope && !scope[:conditions].blank?
+ segments << sanitize_sql(conditions) unless conditions.blank?
segments << type_condition unless descends_from_active_record?
segments.compact!
- sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty?
+ sql << "WHERE (#{segments.join(") AND (")}) " unless segments.all?(&:blank?)
end
def type_condition
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 1c4b9d4777..de7d1b4318 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -1,4 +1,5 @@
require 'abstract_unit'
+require 'fixtures/comment'
require 'fixtures/company'
require 'fixtures/topic'
require 'fixtures/reply'
@@ -128,6 +129,18 @@ class FinderTest < Test::Unit::TestCase
assert topic.attribute_present?("author_name")
assert topic.respond_to?("author_name")
end
+
+ def test_find_on_blank_conditions
+ [nil, " ", [], {}].each do |blank|
+ assert_nothing_raised { Topic.find(:first, :conditions => blank) }
+ end
+ end
+
+ def test_find_on_blank_bind_conditions
+ [ [""], ["",{}] ].each do |blank|
+ assert_nothing_raised { Topic.find(:first, :conditions => blank) }
+ end
+ end
def test_find_on_array_conditions
assert Topic.find(1, :conditions => ["approved = ?", false])