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]
@ticket.save

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
the
Ticket model’s total_amount and direct_amount database fields are
decimals
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).
I
would expect you to get exceptions like:

TypeError: String can’t be coerced into BigDecimal

or

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
the
web browser, Rails has no way of knowing that params[:amount] should be
a
number.

Now, I don’t see how you would be getting a “can’t copy BigDecimal”
error
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
will
happen if you try to #dup or #clone a BigDecimal, but I don’t see you
doing
that.

Thanks for any help.
–Greg

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs