Permalink
Browse files

Merge pull request #61 from benpickles/missing-column-error

Raise a useful message when passing validate: false and a value doesn't have a corresponding column
  • Loading branch information...
zdennis committed Aug 20, 2012
2 parents a1a183e + f12d72b commit 2bb1150e00cd9f01243a03d53ecd02fd82c775da
Showing with 15 additions and 0 deletions.
  1. +9 −0 lib/activerecord-import/import.rb
  2. +6 −0 test/import_test.rb
@@ -17,6 +17,12 @@ def supports_on_duplicate_key_update? #: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 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
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
View
@@ -46,6 +46,12 @@
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

0 comments on commit 2bb1150

Please sign in to comment.