Jaime JB
2007-Dec-31 15:31 UTC
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gregory Seidman
2007-Dec-31 15:45 UTC
Re: Need Help, getting Big Decimal error on a simple calculation
On Mon, Dec 31, 2007 at 04:31:40PM +0100, Jaime JB wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---