Skip to content

Commit

Permalink
Merge pull request #65 from jabley/missing-column
Browse files Browse the repository at this point in the history
Provide more detailed error message
  • Loading branch information
zdennis committed Sep 14, 2012
2 parents 6182b94 + b03af0c commit c08d31b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/activerecord-import/import.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c08d31b

Please sign in to comment.