我有一个人模型:
class Person
include Mongoid::Document
include Mongoid::MultiParameterAttributes
field :dob, :type => Date
end
我date从 csv 文件中获取我的值作为字符串:
“1990-10-23”
如何将它们保存在数据库中?
我试过了
Date.parse(""1990-10-23")
但它给出了Invalid date错误。
我有一个人模型:
class Person
include Mongoid::Document
include Mongoid::MultiParameterAttributes
field :dob, :type => Date
end
我date从 csv 文件中获取我的值作为字符串:
“1990-10-23”
如何将它们保存在数据库中?
我试过了
Date.parse(""1990-10-23")
但它给出了Invalid date错误。
您的字段中有错字(不必要的冒号)。你想要的是:
field :dob, :type => Date
然后,您将能够执行以下操作:
Person.create(:dob => Date.new(1981, 1, 1))
Mongoid 中的类型始终是类(不是符号)。支持的类的完整列表在这里。
我终于找到了在rails中使用它的方法。
"1990-10-23".to_time(:local)
> 1990-10-23 00:00:00 +0530
"1990-10-23 11:00 PM".to_time(:utc)
> 1990-10-23 23:00:00 +0530
欢迎任何其他方法。