From b03af0cd3e089f09641570f20a9b9d08f6117183 Mon Sep 17 00:00:00 2001 From: James Abley Date: Fri, 14 Sep 2012 14:01:00 +0100 Subject: [PATCH] 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. --- lib/activerecord-import/import.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index 343a1c0..a596a4c 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -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