Skip to content

Commit

Permalink
Merge pull request rails#9220 from robertomiranda/where-with-empty-hash
Browse files Browse the repository at this point in the history
Active Record: Change behaviour with empty hash in where clause
  • Loading branch information
NZKoz committed Feb 8, 2013
2 parents f163749 + e170014 commit bcf0e08
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* Raise ArgumentError instead of generate invalid SQL when empty hash is used in where clause value

Roberto Miranda

* Quote numeric values being compared to non-numeric columns. Otherwise,
in some database, the string column values will be coerced to a numeric
allowing 0, 0.0 or false to match any string starting with a non-digit.
Expand Down
Expand Up @@ -8,7 +8,7 @@ def self.build_from_hash(klass, attributes, default_table)

if value.is_a?(Hash)
if value.empty?
queries << '1 = 2'
raise ArgumentError, "Condition value in SQL clause can't be an empty hash"
else
table = Arel::Table.new(column, default_table.engine)
association = klass.reflect_on_association(column.to_sym)
Expand Down
8 changes: 6 additions & 2 deletions activerecord/test/cases/relation/where_test.rb
Expand Up @@ -92,15 +92,19 @@ def test_where_with_table_name
end

def test_where_with_table_name_and_empty_hash
assert_equal 0, Post.where(:posts => {}).count
assert_raises(ArgumentError) do
Post.where(:posts => {})
end
end

def test_where_with_table_name_and_empty_array
assert_equal 0, Post.where(:id => []).count
end

def test_where_with_empty_hash_and_no_foreign_key
assert_equal 0, Edge.where(:sink => {}).count
assert_raises(ArgumentError) do
Edge.where(:sink => {}).count
end
end

def test_where_with_blank_conditions
Expand Down

0 comments on commit bcf0e08

Please sign in to comment.