我在迁移中指定什么数据类型才能使用 FasterCSV 从 CSV 导入日期?
各位溢出者,
我有一个制表符分隔的 csv 文件,其中包含以下格式的日期: 30-11-2011 2:24
我正在使用 FasterSCV
gem 解析 csv。我创建了一个迁移,将此信息存储为日期时间。我应该这样做还是将其定义为 string
并让 Ruby
每次检索它时都将其转换?
我是否需要定义一些 FasterCSV
转换器?
谢谢...
Fellow Overflowers,
I have a tab delimited csv file which contains dates in this format:30-11-2011 2:24
I am parsing the csv with FasterSCV
gem. I have created a migration which stores this info as datetime
. Should I be doing that or define it as string
and let Ruby
convert it each time i retrieve it?
Do I need maybe to define some FasterCSV
converters?
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
日期时间有点棘手。是有严格的格式
但你可以这样做
现在你有一个 DateTime 对象。但是,请注意解析方法末尾的时区
当您填充数据库时,请使用 db 文件夹中的 seeds.rb 文件。
我通常在 db 文件夹中创建一个文本文件,使用我的编辑器并用“|”替换所有选项卡并为每一行创建一个数组
假设它看起来像这样(假设它是一个待办事项列表)
待办事项文件
seeds.rb 文件
,然后运行
Make a some ruby magic!
我还没有测试过这段代码,但它可能会推动你走向正确的方向
DateTime is a bit tricky. Is has a strict format
But you can do this
Now you have a DateTime object. But, pay attention to the time zone at the end of the parse method
When you fill the database use the seeds.rb file in your db folder.
I usually make a text file in the db folder, use my editor and replace all tabs with a "|" and make an array of each line
Lets say it looks like this (asuming it is a todo list)
todo file
seeds.rb file
then run
Make a some ruby magic!
I have not tested this code, but it might push you in the right direction