# Validation :if option with Class vars

**URL:** <https://www.ruby-forum.com/t/validation-if-option-with-class-vars/195341>\
**Category:** Rails\
**Created:** [September 21, 2010, 11:10pm UTC](https://www.ruby-forum.com/t/validation-if-option-with-class-vars/195341 "2010-09-21T23:10:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [September 21, 2010, 11:10pm UTC](https://www.ruby-forum.com/t/validation-if-option-with-class-vars/195341/1 "2010-09-21T23:10:06Z")

</div>

I have some validations that need to include an :if clause using the  
Class and not the model.

Sounds reasonably straight forward at first by doing this:

validates\_presence\_of :field\_one, :field\_two, :if =\>  
Proc.new{|MyClass| MyClass.feature\_is\_enabled}

And that works. The problem occurs when I do mor ethan one validation  
that way.

validates\_presence\_of :field\_one, :field\_two, :if =\>  
Proc.new{|MyClass| MyClass.feature\_is\_enabled}  
validates\_numericality\_of :field\_three, :field\_four, :if =\>  
Proc.new{|MyClass| MyClass.feature\_is\_enabled}

That generates a “constant already defined” error. I suspect it has to  
be complaining about the |MyClass| part, but I can’t think of what else  
to do there.

Any ideas? Thx.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [September 22, 2010, 7:28am UTC](https://www.ruby-forum.com/t/validation-if-option-with-class-vars/195341/2 "2010-09-22T07:28:50Z")

</div>

just modify the condition as follows:

:if =\> Proc.new{|my\_class| MyClass.feature\_is\_enabled}  
(or)  
:if =\>Proc.new{|whatever| MyClass.feature\_is\_enabled}

The problem is you used MyClass as constant instead of variable. Just  
using the lowercase letter will solve ur problem.

–  
Regards,

T.Veerasundaravel.  
[http://tinyurl.com/25vma7h](http://tinyurl.com/25vma7h)  
@veerasundaravel

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [September 24, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/validation-if-option-with-class-vars/195341/3 "2010-09-24T21:55:40Z")

</div>

Veera S. wrote:

> just modify the condition as follows:
> 
> :if =\> Proc.new{|my\_class| MyClass.feature\_is\_enabled}  
> (or)  
> :if =\>Proc.new{|whatever| MyClass.feature\_is\_enabled}
> 
> The problem is you used MyClass as constant instead of variable. Just  
> using the lowercase letter will solve ur problem.

Duh, that makes sense. I was stuck on needing to pass the class name in,  
which even if that were the case, this isn’t how to do it anyway. Brain  
spasm.

Thx.
