Permalink
Browse files

find gracefully copes with blank :conditions. Closes #7599.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9dac6b7 commit 2be3a33f32b43287b692be2cc2550f159b00165e @jeremy jeremy committed May 26, 2007
Showing with 18 additions and 3 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +3 −3 activerecord/lib/active_record/base.rb
  3. +13 −0 activerecord/test/finder_test.rb
View
@@ -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]
@@ -1249,11 +1249,11 @@ def add_joins!(sql, options, scope = :auto)
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
@@ -1,4 +1,5 @@
require 'abstract_unit'
+require 'fixtures/comment'
require 'fixtures/company'
require 'fixtures/topic'
require 'fixtures/reply'
@@ -128,6 +129,18 @@ def test_find_only_some_columns
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])

0 comments on commit 2be3a33

Please sign in to comment.