diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-30 19:31:31 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-30 19:31:31 +0000 |
commit | 11dc44ac339199680704b52402fd53f2e3c0ee07 (patch) | |
tree | 725a8062020f435f6795d38a0ae993239c6ef1df | |
parent | e2dad56caae7ed7697f8d9b4e9d6a62faa308046 (diff) | |
download | rails-11dc44ac339199680704b52402fd53f2e3c0ee07.tar.gz rails-11dc44ac339199680704b52402fd53f2e3c0ee07.tar.bz2 rails-11dc44ac339199680704b52402fd53f2e3c0ee07.zip |
Fix the generated SQL when In predicate is supplied an empty Array
-rw-r--r-- | lib/arel/engines/sql/core_extensions/array.rb | 6 | ||||
-rw-r--r-- | spec/arel/engines/sql/unit/predicates/in_spec.rb | 19 |
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/arel/engines/sql/core_extensions/array.rb b/lib/arel/engines/sql/core_extensions/array.rb index 80041cb5f3..72f579b7eb 100644 --- a/lib/arel/engines/sql/core_extensions/array.rb +++ b/lib/arel/engines/sql/core_extensions/array.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 diff --git a/spec/arel/engines/sql/unit/predicates/in_spec.rb b/spec/arel/engines/sql/unit/predicates/in_spec.rb index 691abcb2d2..76e5fc58df 100644 --- a/spec/arel/engines/sql/unit/predicates/in_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/in_spec.rb @@ -45,6 +45,25 @@ module Arel 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 |