Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 247
user3576036 Avatar asked Oct 14 '25 14:10

user3576036


1 Answers

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

like image 162
Sander Garretsen Avatar answered Oct 17 '25 02:10

Sander Garretsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!