[英]How to keep leading zeros when saving string to PostgreSQL using Active Record
Why does Rails strip leading zeros when trying to save a numeric value in Active Record?尝试在 Active Record 中保存数值时,为什么 Rails 会去掉前导零?
For instance:例如:
self.dps_billing_id = "0000060010123363"
self.save!
actually saves 60010123363
.实际上节省了
60010123363
。
The column is just a standard string field in a PostgreSQL database:该列只是 PostgreSQL 数据库中的标准字符串字段:
t.string "dps_billing_id"
I need the leading zeros to be stored because my credit-card gateway requires them, but I also cannot simply append a few zeros to the result because the amount can change.我需要存储前导零,因为我的信用卡网关需要它们,但我也不能简单地在结果中附加几个零,因为数量可能会改变。
How do I stop Rails trimming the zeros?如何停止 Rails 修整零?
It may be because your dps_billing_id
is of type integer, not string.这可能是因为您的
dps_billing_id
是整数类型,而不是字符串类型。
In my Rails console:在我的 Rails 控制台中:
u = User.first
u.login_count = '0008' # but login_count is is defined as type integer
u.save
u.reload
ap u # :login_count => 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.