lundi 7 mars 2016

Disable a validation rule in ruby model

I am working on a rails 3 application that uses an external gem for setting up and controlling a settings model (http://ift.tt/1W32I3j).

The gem has some validation in the "setting_methods.rb" program that sets a validates presence to true. Since the application was written there is a new requirement to allow for settings to have empty vales (it will be used to hold an optional setting which may or may not be populated).

Normally, this would be easy enough to do by simply removing the validation from the model (or adding an allow blanks). I can't do that because it's embedded in the gem, unless I change the gem. I can't skip the validation on save because that would ignore the validation I want to run:

model.save(:validate => false)

I could go for the data driven approach and set the value to "nothing" and look for that when deciding to set the optional parameter.

I also had a thought of deleting the error produced by the validation somehow but that seemed to be awfully messy.

So to the question. Is there any way to remove model validation at runtime with code?

Aucun commentaire:

Enregistrer un commentaire