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
This commit is contained in:
Zach Dennis 2012-08-20 05:53:45 -07:00
commit 2bb1150e00
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