Validation :if option with Class vars

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

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
@veerasundaravel

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs