Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The gem doesn't shout when validations fail, silently ignore the inserts and proceeds #110

Closed
navneet-flipkart opened this issue Aug 2, 2013 · 2 comments

Comments

@navneet-flipkart
Copy link

Hi,

I noticed this issue with gem 'activerecord-import', '0.3.1'

While running tests over my API seems like the inserts fail silently when I have validations w.r.t. some fields presence.

validates_presence_of :attr1, :attr2

Doesn't the gem throw an error if the above validation fails? ActiveRecord does throw an error in such cases.

@Gedrovits
Copy link

If you check the debugger, you can see that it performs 'Class Create Many Without Validations Or Callbacks', so to do some checks or whatever, you must implement your own callbacks / handling. If I am not mistaken.

You can try wrap 'import' statement inside transaction, I think this will give you more flexibility on error handling.

@jkowens
Copy link
Collaborator

jkowens commented Oct 6, 2016

The #import method returns an object that includes an array of objects that failed validation, so that can be checked to see if any were unsuccessful:

result = Post.import posts
if result.failed_instances.any?
   # ...
end

As of version 0.13.0 there is also an #import! method which will raise an exception if a validation error occurs. This means that nothing will be inserted if any objects fail validation.

@jkowens jkowens closed this as completed Oct 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants