From 2a2bc8e84aa619089abcc4f9a4c2711a118e5fa9 Mon Sep 17 00:00:00 2001 From: Pratik Naik <pratiknaik@gmail.com> Date: Mon, 18 Jan 2010 12:55:23 +0530 Subject: Handle invalid query IN() generated when a blank array is supplied in hash conditions --- activerecord/lib/active_record/relation/predicate_builder.rb | 3 ++- activerecord/test/cases/relations_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 03f194c462..9e855209f9 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -24,7 +24,8 @@ module ActiveRecord case value when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope - attribute.in(value.to_a) + values = value.to_a + values.any? ? attribute.in(values) : attribute.eq(nil) when Range # TODO : Arel should handle ranges with excluded end. if value.exclude_end? diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index e31d0ee3e8..d34c9b4895 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -339,6 +339,11 @@ class RelationTest < ActiveRecord::TestCase assert_raises(ActiveRecord::RecordNotFound) { authors.find(['42', 43]) } end + def test_find_in_empty_array + authors = Author.scoped.where(:id => []) + assert authors.all.blank? + end + def test_exists davids = Author.where(:name => 'David') assert davids.exists? -- cgit v1.2.3