From f12d72b0b19e959a568dedaf54ae10009570877c Mon Sep 17 00:00:00 2001 From: Ben Pickles Date: Thu, 16 Aug 2012 20:22:49 +0100 Subject: [PATCH] Raise a useful message when a value doesn't have a corresponding column. --- lib/activerecord-import/import.rb | 9 +++++++++ test/import_test.rb | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index c18dad8..c4e4c3c 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -17,6 +17,12 @@ module ActiveRecord::Import #:nodoc: true end end + + class MissingColumnError < StandardError + def initialize(index) + super "Missing column for value at index #{index}" + end + end end class ActiveRecord::Base @@ -297,6 +303,9 @@ class ActiveRecord::Base array_of_attributes.map do |arr| my_values = arr.each_with_index.map do |val,j| column = columns[j] + + raise ActiveRecord::Import::MissingColumnError.new(j) if column.nil? + if val.nil? && !sequence_name.blank? && column.name == primary_key connection.next_value_for_sequence(sequence_name) else diff --git a/test/import_test.rb b/test/import_test.rb index d25e069..094dd97 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -46,6 +46,12 @@ describe "#import" do result = Topic.import columns, invalid_values, :validate => false end end + + it 'should raise a specific error if a column does not exist' do + assert_raises ActiveRecord::Import::MissingColumnError do + Topic.import ['foo'], [['bar']], :validate => false + end + end end context "with validation checks turned on" do