在 Rails 模型中使用持续时间字段
我正在寻找在 Rails 模型中使用持续时间字段的最佳方法。 我希望格式为 HH:MM:SS(例如:01:30:23)。 本地使用的数据库是 sqlite,生产中使用的数据库是 Postgres。
我还想研究这个领域,这样我就可以查看该领域中的所有对象,并得出该模型中所有对象的总时间,最后得到如下结果:
30 条记录,总计 45 小时 25 分 34 秒。
那么什么最适合呢?
- 迁移的字段类型
- CRUD 表单的表单字段(小时、分钟、秒下拉列表?)
- 生成模型中所有记录的总持续时间的最便宜的方法
I'm looking for the best way to use a duration field in a Rails model. I would like the format to be HH:MM:SS (ex: 01:30:23). The database in use is sqlite locally and Postgres in production.
I would also like to work with this field so I can take a look at all of the objects in the field and come up with the total time of all objects in that model and end up with something like:
30 records totaling 45 hours, 25 minutes, and 34 seconds.
So what would work best for?
- Field type for the migration
- Form field for the CRUD forms (hour, minute, second drop downs?)
- Least expensive method to generate the total duration of all records in the model
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SUM
查询即可生成总计。 如果您使用整数,这既简单又快速。另外:
123.seconds
(将123
替换为数据库中的整数)轻松将秒数整数形式的持续时间转换为ActiveSupport::Duration
)。 对生成的Duration
使用inspect
以获得良好的格式。 (它并不完美。您可能想自己编写一些东西。)duration=(new_duration)
和duration
,它们会使用整数参数在内部调用read_attribute
/write_attribute
。SUM
query over the duration column to produce a grand total. If you use integers, this is easy and fast.Additionally:
ActiveSupport::Duration
by using123.seconds
(replace123
with the integer from the database). Useinspect
on the resultingDuration
for nice formatting. (It is not perfect. You may want to write something yourself.)ActiveSupport::Duration
objects, rather than integers. Simply defineduration=(new_duration)
andduration
, which internally callread_attribute
/write_attribute
with integer arguments.在 Rails 5 中,您可以使用 ActiveRecord::Attributes 将 ActiveSupport::Durations 存储为 ISO8601 字符串。 与整数相比,使用 ActiveSupport::Duration 的优点是您可以直接使用它们进行日期/时间计算。 您可以执行诸如
Time.now + 1.month
之类的操作,它总是正确的。操作方法如下:
添加
config/initializers/duration_type.rb
迁移
模型
用法
In Rails 5, you can use ActiveRecord::Attributes to store ActiveSupport::Durations as ISO8601 strings. The advantage of using ActiveSupport::Duration over integers is that you can use them for date/time calculations right out of the box. You can do things like
Time.now + 1.month
and it's always correct.Here's how:
Add
config/initializers/duration_type.rb
Migration
Model
Usage
我尝试使用 ActiveSupport::Duration 但无法使输出清晰。
您可能喜欢 ruby-duration,它是一种不可变类型,表示一定的时间量,精确到秒。 它有很多测试和 Mongoid 模型字段类型。
我还想轻松解析人类持续时间字符串,因此我选择了 慢性持续时间。 以下是将其添加到具有 time_spent(以秒为单位)字段的模型的示例。
I tried using ActiveSupport::Duration but had trouble getting the output to be clear.
You may like ruby-duration, an immutable type that represents some amount of time with accuracy in seconds. It has lots of tests and a Mongoid model field type.
I wanted to also easily parse human duration strings so I went with Chronic Duration. Here's an example of adding it to a model that has a time_spent in seconds field.
我编写了一些存根来支持和使用 PostgreSQL 的
interval
类型作为ActiveRecord::Duration
。请参阅此要点(您可以在 Rails 4.1 中将其用作初始化程序): https://gist.github.com/ Envek/7077bfc36b17233f60ad
此外,我还向那里的 Rails 开放了拉取请求:
https://github.com/rails/rails/pull/16919
I've wrote a some stub to support and use PostgreSQL's
interval
type asActiveRecord::Duration
.See this gist (you can use it as initializer in Rails 4.1): https://gist.github.com/Envek/7077bfc36b17233f60ad
Also I've opened pull requests to the Rails there:
https://github.com/rails/rails/pull/16919