1

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
4

2 に答える 2

1

Look here: TimeWithZone has a constructor, which takes a normal Time object in utc and a timezone. So, given time is you can try this:

ActiveSupport::TimeWithZone.new(Chronic.parse(self.natural_date).utc, Time.zone)
于 2010-11-09T19:48:26.180 に答える
1

Time.zoneにはparseメソッドがあり、次の値も返しますActiveSupport::TimeWithZone:

>> Time.zone.parse "October 4 1984"
=> Thu, 04 Oct 1984 00:00:00 EDT -04:00

Chronic で遊ぶには、この記事が役立つかもしれません。たとえば、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-09T19:30:23.490 に答える