Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to skip validations on update_attributes?

This seems like a silly problem but I can't find the answer anywhere!

Is it possible to update_attributes() and skip validations, like in save(validate: false)?

I have a long form with some lengthy text (not string) fields, and I'd like to offer the user the ability to save their progress on the form. Normally, I want length minimums, etc, on the answers before they're able to submit it and move on, but in the case where they're just clicking "Save" I'd like to put whatever progress they've made in the database so they can come back and finish later. Is there a way to skip the validations in this case?

From looking around, it seems the only thing I can do is enumerate each field individually like so:

@obj.field1 = ...
@obj.field2 = ...
@obj.field3 = ...
...
@obj.save(:validate => false)

Is that really the case? I'd be worried about adding another field at some point in the future and forgetting to update the controller here.

like image 545
Gabe Durazo Avatar asked Apr 09 '12 20:04

Gabe Durazo


People also ask

What are validations in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.


1 Answers

@obj.attributes = params[:obj]
@obj.save(false)

Update for Rails 3

@obj.attributes = params[:obj]
@obj.save(:validate => false)
like image 89
fl00r Avatar answered Oct 19 '22 19:10

fl00r