Skip to content

Commit

Permalink
Merge pull request #6971 from dmathieu/empty_hash
Browse files Browse the repository at this point in the history
fix querying with an empty hash
  • Loading branch information
rafaelfranca committed Sep 19, 2012
2 parents bf4c8fe + 30a576f commit 5dc5560
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##

* Fix querying with an empty hash *Damien Mathieu*

* Fix creation of through association models when using `collection=[]`
on a `has_many :through` association from an unsaved model.
Fix #7661.
Expand Down
8 changes: 6 additions & 2 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ def self.build_from_hash(klass, attributes, default_table)
table = Arel::Table.new(column, default_table.engine)
association = klass.reflect_on_association(column.to_sym)

value.each do |k, v|
queries.concat expand(association && association.klass, table, k, v)
if value.empty?
queries.concat ['1 = 2']
else
value.each do |k, v|
queries.concat expand(association && association.klass, table, k, v)
end
end
else
column = column.to_s
Expand Down
11 changes: 10 additions & 1 deletion activerecord/test/cases/relation/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
require 'models/treasure'
require 'models/post'
require 'models/comment'
require 'models/edge'

module ActiveRecord
class WhereTest < ActiveRecord::TestCase
fixtures :posts
fixtures :posts, :edges

def test_belongs_to_shallow_where
author = Author.new
Expand Down Expand Up @@ -76,5 +77,13 @@ def test_where_with_table_name
post = Post.first
assert_equal post, Post.where(:posts => { 'id' => post.id }).first
end

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

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

0 comments on commit 5dc5560

Please sign in to comment.