Skip to content
This repository
Browse code

Merge pull request #9220 from robertomiranda/where-with-empty-hash

Active Record: Change behaviour with empty hash in where clause
  • Loading branch information...
commit bcf0e08a8df372ef1be376587e5bc206c740e5ef 2 parents f163749 + e170014
Michael Koziarski authored February 07, 2013
4  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,9 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Raise ArgumentError instead of generate invalid SQL when empty hash is used in where clause value
  4
+
  5
+    Roberto Miranda
  6
+
3 7
 *   Quote numeric values being compared to non-numeric columns. Otherwise,
4 8
     in some database, the string column values will be coerced to a numeric
5 9
     allowing 0, 0.0 or false to match any string starting with a non-digit.
2  activerecord/lib/active_record/relation/predicate_builder.rb
@@ -8,7 +8,7 @@ def self.build_from_hash(klass, attributes, default_table)
8 8
 
9 9
         if value.is_a?(Hash)
10 10
           if value.empty?
11  
-            queries << '1 = 2'
  11
+            raise ArgumentError, "Condition value in SQL clause can't be an empty hash"
12 12
           else
13 13
             table       = Arel::Table.new(column, default_table.engine)
14 14
             association = klass.reflect_on_association(column.to_sym)
8  activerecord/test/cases/relation/where_test.rb
@@ -92,7 +92,9 @@ def test_where_with_table_name
92 92
     end
93 93
 
94 94
     def test_where_with_table_name_and_empty_hash
95  
-      assert_equal 0, Post.where(:posts => {}).count
  95
+      assert_raises(ArgumentError) do
  96
+        Post.where(:posts => {})
  97
+      end
96 98
     end
97 99
 
98 100
     def test_where_with_table_name_and_empty_array
@@ -100,7 +102,9 @@ def test_where_with_table_name_and_empty_array
100 102
     end
101 103
 
102 104
     def test_where_with_empty_hash_and_no_foreign_key
103  
-      assert_equal 0, Edge.where(:sink => {}).count
  105
+      assert_raises(ArgumentError) do
  106
+        Edge.where(:sink => {}).count
  107
+      end
104 108
     end
105 109
 
106 110
     def test_where_with_blank_conditions

0 notes on commit bcf0e08

Please sign in to comment.
Something went wrong with that request. Please try again.