Can someone please tell me the best approach to adding support for editing MySQL decimal types in rails? I have a field in the DB that represents currency, and the view will not let save anything past the decimal place so, 10.99 becomes 10.00 automatically. Any help is appreciated.
on 2006-05-04 17:56
on 2006-05-04 18:13
You have to name it :float instead of :integer in your database. On 5/4/06, M. <email@example.com> wrote: > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > firstname.lastname@example.org > http://lists.rubyonrails.org/mailman/listinfo/rails > --
on 2006-05-04 18:22
Oh and of course the variables you put into the database have to be float also. As Integer in Ruby does not include decimals either. So if something is integer you are better of converting to to float before. Example @order.price = @number.to_f + @vat On 5/4/06, Jon Gretar B. <email@example.com> wrote: > > Any help is appreciated. > -- > -------------- > Jon Gretar B. > http://www.jongretar.net/ > --
on 2006-05-05 00:58
M. wrote: > Can someone please tell me the best approach to adding support for > editing MySQL decimal types in rails? You can for example use long-decimal as ruby-type to match mysql decimal types with fixed number of digits after the decimal point. gem install long-decimal in ruby require "rubygems" require_gem "long-decimal" ... > I have a field in the DB that represents currency, and the view will not > let save anything past the decimal place so, 10.99 becomes 10.00 > automatically. You should be aware that there are other approaches. There is a Money-type in Ruby, which might be useful for you. I have some doubts if this is the right way to go, because Money combines the currency and the amount, which could be two different fields in the database. But it might work in your case. I would not recommend using Float for monetary stuff, because it has uncontrolled rounding behaviour. BigDecimal would be a possibility, but it is not an exact match to the database type. Best regards, Karl