[Rails] Validating composed_of

Jordan Elver
Tue, 11 Jul 2006 14:29:23 -0000

Hi,
How do I validate a composed_of attribute in my model?

I have a product model with:

	composed_of :price, :class_name => ''Money'', :mapping =>
%w(cents cents)

This maps the price attribute to the Money
(http://dist.leetsoft.com/api/money/) class. I want to make sure that
the price only contains a price to 2 decimal places i.e. 1.25. This is
because if someone enters 1.256 then the price ends being 1.26.

The standard validations that I''ve tried don''t work.
validates_numericality_of complains of a lack of
"price_before_type_cast".

How do you validate these attributes?

Many thanks,
Jordan