Thursday, 12 September 2013

How to make Rails' format with validator dynamic?

How to make Rails' format with validator dynamic?

I am trying to validate the format of exchange_rate in my Invoice class:
class Invoice < ActiveRecord::Base
attr_accessible :currency, :exchange_rate
validates :exchange_rate, :format => { :with => exchange_rate_format }
private
def exchange_rate_format
if currency != user.preference.base_currency
DECIMAL_REGEX
else
ANOTHER_REGEX
end
end
end
The problem is: It doesn't work at all. I guess I need to use a Proc here?
I never really figured out how to use it though. Maybe somebody can help.
Thanks a lot.

No comments:

Post a Comment