Raise a useful message when a value doesn't have a corresponding column.
This commit is contained in:
parent
a1a183e43d
commit
f12d72b0b1
|
@ -17,6 +17,12 @@ module ActiveRecord::Import #:nodoc:
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class MissingColumnError < StandardError
|
||||||
|
def initialize(index)
|
||||||
|
super "Missing column for value at index #{index}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActiveRecord::Base
|
class ActiveRecord::Base
|
||||||
|
@ -297,6 +303,9 @@ class ActiveRecord::Base
|
||||||
array_of_attributes.map do |arr|
|
array_of_attributes.map do |arr|
|
||||||
my_values = arr.each_with_index.map do |val,j|
|
my_values = arr.each_with_index.map do |val,j|
|
||||||
column = columns[j]
|
column = columns[j]
|
||||||
|
|
||||||
|
raise ActiveRecord::Import::MissingColumnError.new(j) if column.nil?
|
||||||
|
|
||||||
if val.nil? && !sequence_name.blank? && column.name == primary_key
|
if val.nil? && !sequence_name.blank? && column.name == primary_key
|
||||||
connection.next_value_for_sequence(sequence_name)
|
connection.next_value_for_sequence(sequence_name)
|
||||||
else
|
else
|
||||||
|
|
|
@ -46,6 +46,12 @@ describe "#import" do
|
||||||
result = Topic.import columns, invalid_values, :validate => false
|
result = Topic.import columns, invalid_values, :validate => false
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
context "with validation checks turned on" do
|
context "with validation checks turned on" do
|
||||||
|
|
Loading…
Reference in a new issue