Need Help, getting Big Decimal error on a simple calculation

Ok this is driving me nuts.

Here’s my issue in it’s most basic form.

I have a table with a column called amount that was set to :decimal,
:precision => 8, :scale => 2

In my controller I try to do something like this.

id = 1
@ticket = Ticket.find(:first, :conditions => [‘id=?’,id])
@ticket.total_amount += params[:amount]
@ticket.direct_amount += params[:amount]

This returns an error saying that it ‘can’t copy BigDecimal’

If I try to user .to_f on my total_amount and direct_amount and
params[:amount] I just end up getting an error about float allocation.

Any ideas? What info can I provide to make this more helpful?

Thanks for any help.

On Mon, Dec 31, 2007 at 04:31:40PM +0100, Jaime JB wrote:

id = 1
Any ideas? What info can I provide to make this more helpful?
The actual exception/error messages would be helpful. I’m assuming that
Ticket model’s total_amount and direct_amount database fields are
and therefore ActiveRecord treats them as BigDecimal objects as well as
this amount column you mentioned (which table is it in? You don’t say).
would expect you to get exceptions like:

TypeError: String can’t be coerced into BigDecimal


TypeError: nil can’t be coerced into BigDecimal

The values in the params hash are always nil, a string, a hash, or an
array. Since params comes from the parameters sent as text via HTTP by
web browser, Rails has no way of knowing that params[:amount] should be

Now, I don’t see how you would be getting a “can’t copy BigDecimal”
given the code you show. You should examine the stacktrace and make sure
that the exception is occurring where you think it is. That exception
happen if you try to #dup or #clone a BigDecimal, but I don’t see you

Thanks for any help.