# Programmatically disabling validates\_presence\_of for an inst

**URL:** <https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145>\
**Category:** Rails\
**Created:** [April 5, 2007, 4:04am UTC](https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145 "2007-04-05T04:04:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@ntran](https://www.ruby-forum.com/u/ntran)\
**Post date:** [April 5, 2007, 4:04am UTC](https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145/1 "2007-04-05T04:04:39Z")

</div>

I have a number of models with various data validations, e.g:

class Task \< ActiveRecord::Base

validates\_presence\_of :comment  
validates\_presence\_of :complaint  
validates\_inclusion\_of :status, :in =\> TASK\_STATUSES  
validates\_format\_of :policy\_number…

[…]

end

A user is allowed to save data in an incomplete state, which currently  
disables all validations for the model. Not good. What I want to do is  
programmatically disable validates\_presence\_of for the duration of the  
save on _that instance of the model alone_, whilst still allowing the  
other data format validations to be processed - i.e. temporarily  
overriding a class method with an instance method, or aliasing the  
class method with another class method that checks an instance  
variable to determine whether or not to proceed with the call to  
validates\_presence\_of.

What I read on the web left me a little confused as to issues such as  
thread-safety, etc.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![ntran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@ntran](https://www.ruby-forum.com/u/ntran)\
**Post date:** [April 5, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145/2 "2007-04-05T15:28:44Z")

</div>

I’m not sure I understad what you’re requesting, but you should be  
able to do something like

validates\_presence\_of :comment, :if =\> Proc.new { |task| !task.draft }

In the above, “task” is the current model instance you’re trying to  
save. I don’t think there would be any threading issues…

---

<div class="post-metadata">

**Author:** ![ntran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@ntran](https://www.ruby-forum.com/u/ntran)\
**Post date:** [April 5, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145/3 "2007-04-05T15:58:51Z")

</div>

On 4/4/07, Nick T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> […]  
> variable to determine whether or not to proceed with the call to  
> validates\_presence\_of.
> 
> What I read on the web left me a little confused as to issues such as  
> thread-safety, etc.
> 
> Any suggestions?

Hey Nick,

I wrote a plugin [1] that might help you out. It doesn’t let you  
disable a particular validation, but it lets you define a new set of  
validations on an instance. So you could define all validations  
except for that v\_p\_o.

Pat

[1] [eli.st](http://evang.eli.st/blog/2007/1/20/instance-validations-plugin)

---

<div class="post-metadata">

**Author:** ![ntran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@ntran](https://www.ruby-forum.com/u/ntran)\
**Post date:** [April 6, 2007, 2:22am UTC](https://www.ruby-forum.com/t/programmatically-disabling-validates-presence-of-for-an-inst/91145/4 "2007-04-06T02:22:00Z")

</div>

Thanks for the feedback guys. I was aware of the :if condition on the  
validates\_\* methods, but wanted to avoid maintaining those across a  
number of models and a large number of validations.

The idea is to dynamically disable validates\_presence\_of at the start  
of the “save incomplete” call. And re-enable it once the save is  
complete.

Thanks for the link Pat, I’ll check it out.
