user3576036
user3576036

Reputation: 1425

How to skip validations while creating a object in activerecord

I am working on a existing app. One model has number of fields and they have set validations for many of them. I am trying create a object for testing purpose. I only need couple of fields. How do I skip the validation method and create an object?

class Article 
   validate :article_validation
   #...rest of the model

end

All I want to do is do

Article.create(title: "sfsfsd") in console.

And skip rest of the fields

Upvotes: 4

Views: 7649

Answers (1)

Sander Garretsen
Sander Garretsen

Reputation: 1723

a = Article.new(title: "sfsfsd")
a.save(validate: false)

Note that save also has the ability to skip validations if passed validate: false as an argument. This technique should be used with caution.

http://guides.rubyonrails.org/active_record_validations.html#skipping-validations

Upvotes: 16

Related Questions