[英]How to populate a sql db with t.decimal precision and scale via rails c
t.decimal "eur", precision: 8, scale: 2
This is my Row in the Database table (name is wallet). 这是我在数据库表中的行(名称是钱包)。 How can I populate it with data in the rails console? 如何在Rails控制台中填充数据?
I have tried 我努力了
wallet.eur=10
Ok this one is to easy and doesnt work. wallet.eur=10
好的,这很简单,不起作用。
wallet.eur=BigDecimal.new('154.00')
This one throws something like => #<BigDecimal:56cadb8,'0.154E3',9(18)>
But i cant save it and will stay at 0.0. wallet.eur=BigDecimal.new('154.00')
这个抛出类似=> #<BigDecimal:56cadb8,'0.154E3',9(18)>
但我无法保存,它将保持在0.0。
Any Idea or suggestion? 有任何想法或建议吗?
If dealing with currency maybe money-rails gem might be worthwhile? 如果与货币打交道,也许赚钱的宝石可能值得?
Or something similar just using an integer and storing it in pence/cents and creating helpers to display it. 或者类似的事情,只是使用一个整数并将其存储在便士/分中,并创建帮助程序来显示它。
Plays much nicer than using decimal or worse float. 比使用十进制或更差的浮点数要好得多。
Use the built-in rails facilities, check the NumberHelper documentation for examples. 使用内置的滑轨工具,请查看NumberHelper文档以获取示例。
Use this code to add the migration: 使用以下代码添加迁移:
add_column :items, :price, :decimal, :precision => 8, :scale => 2
and then use this method: 然后使用此方法:
number_to_currency(price, :unit => "€")
If you've got an NoMethodError add this line: 如果您遇到NoMethodError,请添加以下行:
include ActionView::Helpers::NumberHelper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.