I created a rails model class for an existing table. Some of the table fields are decimal fields defined in the Postgres DB as numeric(19,2). When I open the rails console and fetch one of this objects like this:
ExistingTableModel.first.total
#<BigDecimal:4bfd250,'0.692E3',9(18)>
So, I am getting a BigDecimal 9(18) . This is a problem because when I do sums and stuff like that, the results are not nice due to rounding.
How can I force rails to map those fields to a BigDecimal with 2 decimals?
Thanks!
You could override attribute accessor(s). Say you have a legacy Postgres table with an attribute foo numeric(19,2)
This would make every access of the foo attribute rounded to precision of 2 decimal places.