Permalink
Browse files

Merge pull request #65 from jabley/missing-column

Provide more detailed error message
  • Loading branch information...
2 parents 6182b94 + b03af0c commit c08d31b528748724adaca0c13e3c6574b31d5cab @zdennis committed Sep 14, 2012
Showing with 9 additions and 5 deletions.
  1. +9 −5 lib/activerecord-import/import.rb
@@ -19,8 +19,8 @@ def supports_on_duplicate_key_update? #: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 @@ def import_with_validations( column_names, array_of_attributes, options={} )
# 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 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
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

0 comments on commit c08d31b

Please sign in to comment.