Forum: Ruby on Rails Validating numericality based on related attribute value?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matthew L. (Guest)
on 2006-06-08 01:08
(Received via mailing list)
I have a price attribute in my model which I need to validate as a
positive number if the attribute price_terms.requires_value is equal to
'Y', otherwise price can be blank or zero.  Price_terms is a select list
which my model table belongs_to.

How do I do this? - I tried doing this:

validates_numericality_of :price, :if =>
self.price_term.requires_value.upcase == 'Y'

But that doesn't work...

Thanks in advance,

Matt.
Jonathan V. (Guest)
on 2006-06-08 03:38
(Received via mailing list)
Try this:

validates_numericality_of :price, :if => Proc.new { |r|
r.price_term.requires_value.upcase == 'Y' }

-Jonathan.
This topic is locked and can not be replied to.