In rails i have amigration to alter production data to fit new validation rules, There are several things wrong so i have 2 different migrations (they could be one but still two aspects that run separately ) one fails because the other validation is not met and vice versa
the validation is new in the model like
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"
is one kind that is completely new and the other just changed from 24 to 8 and added the overtime bit
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 re ordering the migrations and i got the reverse result.
is there a way other than updating the database first then updating this file to bypass that? or is that what i should be doing?
in Rails 2:
object.save(false)
in Rails 3 & 4:
object.save(:validate => false)
These methods will bypass any and all validations on the object, so be careful!
Hey I know this is an old question and is already answered, but based on your comment I thought I would leave my two cents.
There is no way in Rails 2 or 3 to turn a single validation on or off. However, we use population tasks extensively in our projects so we have a small workaround for the same.
It's a little tedious if you want to do it for every single validation, but generally the one's you want to 'turn off' for a bit are few and far between.
class FooModel < ActiveRecord::Base
validates_uniqueness_of :foo_name, :unless => :dont_check_foo_name
attr_accessor :dont_check_foo_name
end
If you follow a strong naming convention, when you create an object, you can simple set the appropriate dont_check_*validation_name* to true and it will by-pass the validation.
Also, for your second comment, the following:
object.save(false)
object.save!(false)
work in the same way.
And of-course the conditional validation I mentioned works on both as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With