我正在使用Chronic来解析时间,它返回了这个错误:
ArgumentError in EventsController#create
comparison of Date with ActiveSupport::TimeWithZone failed这是因为从Rails 2.1开始,数据库和Ruby处于不同的时区。
如何将我的语句转换为工作?
def set_dates
unless self.natural_date.blank? || Chronic.parse(self.natural_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_date)
self.date = nil
self.time = Chronic.parse(self.natural_date)
else
self.date = Chronic.parse(self.natural_date).to_date
self.time = nil
end
end发布于 2010-11-10 03:30:23
Time.zone有一个parse方法,该方法也返回一个ActiveSupport::TimeWithZone
>> Time.zone.parse "October 4 1984"
=> Thu, 04 Oct 1984 00:00:00 EDT -04:00为了让它和慢性病玩起来,也许this article能帮上忙?例如,如果要将一个parse_with_chronic方法修补到ActiveSupport::TimeZone中,那么可以重写您的方法:
def set_dates
unless self.natural_date.blank? || Time.zone.parse_with_chronic(self.natural_date).blank?
# check if we are dealing with a date or a date + time
if time_provided?(self.natural_date)
self.date = nil
self.time = Time.zone.parse_with_chronic(self.natural_date)
else
self.date = Time.zone.parse_with_chronic(self.natural_date).to_date
self.time = nil
end
end
end发布于 2010-11-10 03:48:26
看这里:TimeWithZone有一个构造函数,它接受世界协调时的普通时间对象和一个时区。因此,在给定的时间内,您可以尝试以下操作:
ActiveSupport::TimeWithZone.new(Chronic.parse(self.natural_date).utc, Time.zone)https://stackoverflow.com/questions/4137103
复制相似问题