简体   繁体   English

使用 Active Record 将字符串保存到 PostgreSQL 时如何保持前导零

[英]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.

相关问题 在postgres /活动记录中保存字符串时,请保持前导零 - Keep leading zeros when saving a string in postgres/active record 如何在Rails yml固定装置中保持前导零? - how to keep leading zeros in Rails yml fixtures? 如何使用Active Record从PostgreSQL类型获取Ruby类型 - How to get Ruby type from PostgreSQL type using Active Record 保存记录时如何保持数组结构-activerecord / postgres - How to keep array structure when saving record - activerecord/postgres 如何在保持适当的尾随/前导零的同时增加字符串中的数字 - How to increment the number in a string while keeping the appropriate trailing/leading zeros 如何使用Active Record查询来计算总字符串值/组? - How to count total string value / group using an Active Record query? 将整数转换为二进制字符串,并用前导零填充 - Convert integer to binary string, padded with leading zeros 如何在Rails中使用Active Record时指定Ruby正则表达式? - How to specify Ruby regex when using Active Record in Rails? 切换到PostgreSQL时Rails活动记录查询中断 - Rails Active Record Query Breaking When Switching To PostgreSQL 查询 postgresql 视图时,Active Record 转换日期不一致 - Active Record transforming dates inconsistently when querying a postgresql view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM