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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0ccfc4935cd968d289decc405604a2ca?d=identicon&s=25 Matthew Law (Guest)
on 2006-06-07 23: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,

631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-06-08 01:38
(Received via mailing list)
Try this:

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

This topic is locked and can not be replied to.