Validation bypass when passing only data to correct validation errors

In rails, I have a migration to modify production data to fit the new validation rules. There are several errors, so I have two different migrations (they can be one, but still two aspects that are performed separately), one of them does not work, because the other check is not met and vice versa

validation is new in type model

validates_uniqueness_of :job_id , :scope => [:day, :time, :user_id , :overtime, :comments] , :message => "Duplicate Entry, Please check your data" validates_uniqueness_of :job_id , :scope => [:day, :user_id, :comments] , :message => "Has 2 Entires for same job on same day with same comment" 

- this is a completely new look and the other just changed from 24 to 8 and added a bit of overtime

  validates_numericality_of :time, :greater_than => 0, :less_than_or_equal_to => 8 validates_numericality_of :overtime, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 16 

I tried reordering migrations, and I got the opposite result.

Is there a way besides updating the database and then updating this file to get around this? or is that what i should do?

+7
source share
2 answers

in Rails 2:

 object.save(false) 

in Rails 3 and 4:

 object.save(:validate => false) 

These methods will bypass any and all object checks, so be careful!

+21
source

Hey, I know this is an old question that I already answered, but based on your comment, I thought I would leave my two cents.

In Rails 2 or 3, there is no way to enable or disable a single check. However, we widely use population tasks in our projects, so we have a small workaround for the same.

It’s a little tedious if you want to do this for each check, but in general the one you want to “disable” for the bits is a bit far.

 class FooModel < ActiveRecord::Base validates_uniqueness_of :foo_name, :unless => :dont_check_foo_name attr_accessor :dont_check_foo_name end 

If you adhere to a strong naming convention when you create the object, you can simply set the corresponding dont_check_ * validation_name * to true and it will bypass the check.

Also, for your second comment, the following:

 object.save(false) object.save!(false) 

work the same way.

And, of course, the conditional validation that I talked about works for both.

+1
source

All Articles