Provide more detailed error message

If we don't have a mapping for a column name, provide the column name
as well as the index in the array.
This commit is contained in:
James Abley 2012-09-14 14:01:00 +01:00
parent 6182b944ef
commit b03af0cd3e

View file

@ -19,8 +19,8 @@ module ActiveRecord::Import #:nodoc:
end
class MissingColumnError < StandardError
def initialize(index)
super "Missing column for value at index #{index}"
def initialize(name, index)
super "Missing column for value <#{name}> at index #{index}"
end
end
end
@ -276,7 +276,13 @@ class ActiveRecord::Base
# information on +column_names+, +array_of_attributes_ and
# +options+.
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
columns = column_names.map { |name| columns_hash[name.to_s] }
columns = column_names.each_with_index.map do |name, i|
column = columns_hash[name.to_s]
raise ActiveRecord::Import::MissingColumnError.new(name.to_s, i) if column.nil?
column
end
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES "
@ -308,8 +314,6 @@ class ActiveRecord::Base
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