处理 Rails 3 中的精确单位

发布于 2024-10-30 00:17:15 字数 324 浏览 1 评论 0原文

有人有经验/有人可以建议存储数量变化较大的数量的最佳实践吗?

例如,我的一个模型中的一个属性用于存储重量,重量可以是从几微克到一公斤或更多。我的假设是将所有内容转换为最小单位并将其作为整数存储在数据库中(因为我可能会因为浮点数而失去准确性?),但以微克为单位存储千克数量似乎很奇怪..

有人能够提出一个建议吗? ruby/rails 插件可能有助于这种行为,在更一般的意义上?就像数据库中的“时间”字段转换为 ruby​​ 中的 Time 对象一样,我将如何最好地拦截数据库条目 ->类属性 过程中,将字段转换为我选择的类?

是否有一种我没有想到的用于存储不同单位大小的设计模式?

Does anyone have experience with / can anyone suggest best practices for storing quantities with large variants in magnitude?

For example, an attribute in one of my models is used to store a weight that could be anything from a few micrograms up to a kilogram or more. My assumption is to convert everything to the smallest unit and store it in the database as an integer (since I might lose accuracy with a float?), but it seems weird to be storing kilogram quantities in micrograms..

Is anybody able to suggest a ruby/rails plugin that might help with this kind of behavior, in the more generic sense? In the same way that a 'time' field in a database is converted into a Time object in ruby, how would I go about best intercepting the database entry -> class attribute process, converting a field into my class of choice?

Is there a design pattern for storing varying unit magnitudes that I'm just not thinking of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-11-06 00:17:15

如果精度很重要,那么您提出的建议就是个好主意。无论是货币还是重量,都存储您可以测量的最小单位的整数。

或者,使用 BigDecimal,它提供对任意精度的支持(如“you-get-to-choose-the- precision”,而不是“Ruby-randomly-chooses-it”)。对存储的良好支持取决于您的数据库 - 例如,PostgreSQL 提供 NUMERIC 数据类型正是用于此目的,并且需要 ORM 的支持(大概是 ActiveRecord,它通过 :decimal 列类型)。

尽管金钱的数量级变化通常小于微克到千克之间的差异,但如果您 像处理金钱一样处理它并适当地设置你的精度。

If precision is important, then what you propose is a good idea. Whether with currency or weights, store the integer amount of the smallest unit you can measure.

Alternatively, use BigDecimal, which provides support for arbitrary precision (as in you-get-to-choose-the-precision, not as in Ruby-randomly-chooses-it). Good support for storing this depends on your database—for example, PostgreSQL provides the NUMERIC data type precisely for this purpose—and requires support from your ORM (presumably ActiveRecord, which supports this in migrations through the :decimal column type).

Even though money typically varies by orders of magnitude smaller than the difference between micrograms to kilograms, you will not run into any problems if you handle this like money and just set your precision appropriately.

ぶ宁プ宁ぶ 2024-11-06 00:17:15

关于时间问题:您不需要为此使用 Rails 插件。您有用于此目的的 Rails 聚合:

Milliseconds with Rails and Mysql (您的问题是故意重复这个?!)

至于精度。这只是使用 DBMS 中可用的最佳类型的问题。

Regarding the Time issue: You don't need a rails plugin for this. You have Rails aggregations for this purpose:

Milliseconds with Rails and Mysql (Is your question an intentional duplicate of this one?!)

As for the precision. That's just a matter of using the best type available in the DBMS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文