Raise a useful message when a value doesn't have a corresponding column.

This commit is contained in:
Ben Pickles 2012-08-16 20:22:49 +01:00
parent a1a183e43d
commit f12d72b0b1
2 changed files with 15 additions and 0 deletions

View file

@ -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

View file

@ -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