diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index 786b79f6..0e6a1ca5 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -42,7 +42,7 @@ def valid_model?(model) model._validate_callbacks.each do |callback| filter = callback.raw_filter - if filter.is_a?(ActiveRecord::Validations::UniquenessValidator) || + if (!@options[:validate_uniqueness] && filter.is_a?(ActiveRecord::Validations::UniquenessValidator)) || (defined?(ActiveRecord::Validations::PresenceValidator) && filter.is_a?(ActiveRecord::Validations::PresenceValidator) && associations.include?(filter.attributes.first)) validate_callbacks.delete(callback) end diff --git a/test/import_test.rb b/test/import_test.rb index aec7fbf9..c3b28c1d 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -210,9 +210,9 @@ end it "should ignore uniqueness validators" do - Topic.import columns, valid_values, validate: true + Topic.import columns, valid_values assert_difference "Topic.count", +2 do - Topic.import columns, valid_values, validate: true + Topic.import columns, valid_values end end @@ -294,6 +294,15 @@ end end end + + context "with uniqueness validators included" do + it "should not import duplicate records" do + Topic.import columns, valid_values + assert_no_difference "Topic.count" do + Topic.import columns, valid_values, validate_uniqueness: true + end + end + end end context "without :validation option" do