从 Rails 在 PostgreSQL 中存储时间间隔 [英] Store time interval in PostgreSQL from Rails
问题描述
我需要在 PosgreSQL 中存储时间 inverval.数据将来自 Ruby on Rails.
I need to store time inverval in PosgreSQL. Data will be put from Ruby on Rails.
比如说,必胜客从 9:00 到 18:00 接受订单.
Say, PizzaHut accepts orders from 9:00 to 18:00.
我创建了以下迁移:
class AddOrderTimeAndDeliveryTimeToMerchants < ActiveRecord::Migration
def self.up
add_column :merchants, :order_time, :interval
end
我已阅读文档,并有以下内容现在代码:
I've read the documentation, and have following code now:
Merchant.create( :delivery_time => "9:00 18:00" )
当我执行它时,我收到以下错误消息:
When i execute it, i get following error message:
PGError: ERROR: invalid input syntax for type interval: "9:00 18:00"
如何正确操作?
推荐答案
我认为间隔并不是您真正想要的.间隔代表没有任何特定终点的时间跨度;例如,您将一个间隔添加到现有时间以获得另一个时间.你最好有两个不同的时间:
I don't think an interval is really what you want for that. An interval represents a timespan without any specific end points; for example, you add an interval to an existing time to get another time. You would be better off with two distinct times:
add_column :merchants, :order_from, :time, :null => false
add_column :merchants, :order_to, :time, :null => false
然后,如果由于某种原因您需要知道他们开放交付的小时数,您可以通过从 :order_to
中减去 :order_from
来构建一个间隔.
Then, if for some reason you need to know how many hours they're open for delivery, you can construct an interval by subtracting :order_from
from :order_to
.
如果你真的必须使用一个区间,那么你需要构造一个这样的值:
If you really must use an interval, then you'll need to construct a value something like this:
:delivery_time => "interval '11 hour'"
注意这说明了一个区间不是从 A 到 B 的特定时间范围,它只是某个特定长度的时间范围(没有指定的结束时间)点).
Note how this illustrates that an interval is not a specific time range from A to B, it is just a time range of some certain length (with no specified end points).
这篇关于从 Rails 在 PostgreSQL 中存储时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!