From d901a7c60fcda115cb3de88b02d0029058720c56 Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Mon, 14 Mar 2011 09:56:51 +0100 Subject: [PATCH] Importing an empty array of objects/values should be supported --- lib/activerecord-import/import.rb | 3 +++ test/import_test.rb | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index 7c889cb..31e9639 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -178,6 +178,9 @@ class ActiveRecord::Base end # end end + # supports empty array + elsif args.last.is_a?( Array ) and args.last.empty? + return ActiveRecord::Import::Result.new([], 0) if args.last.empty? # supports 2-element array and array elsif args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Array ) column_names, array_of_attributes = args diff --git a/test/import_test.rb b/test/import_test.rb index 8e0962d..4512c6d 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -12,6 +12,13 @@ describe "#import" do end end + it "should not produce an error when importing empty arrays" do + assert_nothing_raised do + Topic.import [] + Topic.import %w(title author_name), [] + end + end + context "with :validation option" do let(:columns) { %w(title author_name) } let(:valid_values) { [[ "LDAP", "Jerry Carter"], ["Rails Recipes", "Chad Fowler"]] }