0
如何使用日期时间值并使用HStore存储和检索它们?Rails Hstore dateTime saving
我使用这个:
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)
定义访问器,它除了日期一切都很正常。当我将保存和检索的模型的日期与尚未保存的模型的日期进行比较时,我在测试用例中得到了一些奇怪的东西。基本上保存之前的日期是一个“时间”类,然后是一个字符串。
我意识到hstore将东西保存为字符串,但我很难搞清楚它如何序列化日期,所以我可以在字段的getter/setters中以相同方式反转它。
任何帮助表示赞赏。谢谢。
我试过,但它也给了我的问题。我走的路是我把所有时间戳都保存为iso8601字符串,并添加了一个验证器来传递该格式。我的应用程序主要充当json API,因为我使用客户端的JavaScript为用户界面,所以它会做得很好。当我在数据库中进行特定于日期的查询时,我可能需要进行调整 – DArkO 2013-05-11 08:30:48