Permalink
Browse files

Fix the generated SQL when In predicate is supplied an empty Array

  • Loading branch information...
1 parent e2dad56 commit 11dc44ac339199680704b52402fd53f2e3c0ee07 @lifo lifo committed Jan 30, 2010
Showing with 24 additions and 1 deletion.
  1. +5 −1 lib/arel/engines/sql/core_extensions/array.rb
  2. +19 −0 spec/arel/engines/sql/unit/predicates/in_spec.rb
@@ -2,7 +2,11 @@ module Arel
module Sql
module ArrayExtensions
def to_sql(formatter = nil)
- "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")"
+ if any?
+ "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")"
+ else
+ "(NULL)"
+ end
end
def inclusion_predicate_sql
@@ -45,6 +45,25 @@ module Predicates
end
end
end
+
+ describe 'when the array is empty' do
+ before do
+ @array = []
+ end
+
+ it 'manufactures sql with a comma separated list' do
+ sql = In.new(@attribute, @array).to_sql
+
+ adapter_is :mysql do
+ sql.should be_like(%Q{`users`.`id` IN (NULL)})
+ end
+
+ adapter_is_not :mysql do
+ sql.should be_like(%Q{"users"."id" IN (NULL)})
+ end
+ end
+ end
+
end
describe 'when relating to a range' do

0 comments on commit 11dc44a

Please sign in to comment.