Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Raise a useful message when passing validate: false and a value doesn't have a corresponding column #61

Merged
merged 1 commit into from

2 participants

@benpickles

This results in a more friendly and useful error message than:

NoMethodError: undefined method `type_cast' for nil:NilClass

And is specific to the case where validate: false is passed as ActiveRecord would otherwise raise the error:

NoMethodError: undefined method `foo=' for #<Topic:0x007fd26e5238c8>
@zdennis zdennis merged commit 2bb1150 into zdennis:master
@zdennis
Owner

Thanks @benpickles !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 0 deletions.
  1. +9 −0 lib/activerecord-import/import.rb
  2. +6 −0 test/import_test.rb
View
9 lib/activerecord-import/import.rb
@@ -17,6 +17,12 @@ def supports_on_duplicate_key_update? #:nodoc:
true
end
end
+
+ class MissingColumnError < StandardError
+ def initialize(index)
+ super "Missing column for value at index #{index}"
+ end
+ end
end
class ActiveRecord::Base
@@ -297,6 +303,9 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
array_of_attributes.map do |arr|
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
View
6 test/import_test.rb
@@ -46,6 +46,12 @@
result = Topic.import columns, invalid_values, :validate => false
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
context "with validation checks turned on" do
Something went wrong with that request. Please try again.