Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-Hstore-dateTime-saving_Ruby On Rails_Time_Hstore - Fatal编程技术网

Ruby on rails Rails-Hstore-dateTime-saving

Ruby on rails Rails-Hstore-dateTime-saving,ruby-on-rails,time,hstore,Ruby On Rails,Time,Hstore,如何使用datetime值并使用HStore存储和检索它们 我用的是: module HstoreAccessor def self.included(base) base.extend(ClassMethods) end module ClassMethods def hstore_accessor(hstore_attribute, *keys) Array(keys).flatten.each do |key| define_met

如何使用datetime值并使用HStore存储和检索它们

我用的是:

module HstoreAccessor
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def hstore_accessor(hstore_attribute, *keys)
      Array(keys).flatten.each do |key|
        define_method("#{key}=") do |value|
          send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
          send("#{hstore_attribute}_will_change!")
        end
        define_method(key) do
          send(hstore_attribute) && send(hstore_attribute)[key.to_s]
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, HstoreAccessor)
定义访问器,它适用于除日期之外的所有内容。当我将保存和检索的模型中的日期与尚未保存的模型中的日期进行比较时,我在测试用例中得到了一些wierd信息。基本上,保存前的日期是一个“Time”类,保存后的日期是一个字符串

我意识到hstore将事物保持为字符串,但我很难弄清楚它是如何序列化日期的,这样我就可以在字段的getter/setter中以相同的方式反转日期


感谢您的帮助。谢谢。

它看起来像是使用“to_s”将其序列化,因此将其反序列化回时间实例
Time\35; parse

应该足够了

我尝试过,但也遇到了问题。我的做法是,我将所有时间戳保存为iso8601字符串,并添加了一个验证器来传递该格式。我的应用程序主要充当json API,因为我在UI中使用客户端javascript,所以它可以很好地工作。当我在数据库中进行特定日期的查询时,我可能需要进行调整