decimal(x,y) columns in Rails 1.2 are mapped to BigDecimal object. They
works well, but it could be better.
For example, one column:
for an “articles” table is mapped to a BigDecimal object, but that
object doesn’t “remember” the corresponding column’s :scale.
article = Articles.find(1) article.price # this object is a BigDecimal
one, but it doesn’t knows how many fractional digits must contain
So “article.price.to_s” will not “render” the correct fractional digits
based of the column’s scale.
For example, if we save the 5.20 value into the column, then the
expression “article.price.to_s” will return “5.2”.
In that way, if we save the 5 value, the expression “article.price.to_s”
will return “5.0”.
If the BigDecimal object could “remember” how many decimals it must to
show, then the to_s method could always give you more appropriate
results. In the case of 5.20, to_s will returns “5.20”; in the case of
5, to_s will returns “5.00”. In the same way, if the column were
decimal(5), when you save 5.20 or 5 values, to_s will always returns
The patch I developed tries to solves the problem. With this patch, a
BigDecimal object have a scale attribute, so the to_s method can know
how to print the decimal correctly.
The patch touches files from ActiveRecord and ActiveSupport. The
ActiveRecord part puts the scale into the BigDecimal object, and the
ActiveSupport part prints the BigDecimal object according to the scale
I created a ticket into dev.rubyonrails.org:
In that ticket you can get the patch (in diff format).
The ticket was closed because my implementation has several issues. I do
a call here to all the Rails experts in order to get a better
implementation to get the scale be propagated from the decimal columns
into the BigDecimal objects.
Anybody have an idea to do this?
Thanks a lot.