Backbone、Rails 3.1 和 MongoMapper 的日期时间
我正在学习将 Backbone js 与 Rails 和 Mongo 一起使用。这是我的模型:
class Task
include MongoMapper::Document
include ActionView::Helpers
many :time_entries
key :description, String
key :duration, Integer, :default => 0
timestamps!
end
并且
class TimeEntry
include MongoMapper::EmbeddedDocument
belongs_to :task
key :startDate, Date
key :endDate, Date
end
出于某种原因,我作为 startDate 和 endDate 发送的日期时间在更新 mongo 时会下降,但我可以看到骨干网正在正确地推动它们:
Started PUT "/tasks/4f357ece8af1b04a54000ef2" for 127.0.0.1 at 2012-02-11 12:25:19 -0500
Processing by TasksController#update as JSON
Parameters: {"description"=>"Task with time entry", "time_entries"=>[{"startDate"=>"2012-02-11T17:25:19.529Z"}], "created_at"=>"2012-02-10T20:32:14Z", "duration"=>0, "id"=>"4f357ece8af1b04a54000ef2", "updated_at"=>"2012-02-11T17:25:00Z", "task"=>{"description"=>"Task with time entry", "time_entries"=>[{"startDate"=>"2012-02-11T17:25:19.529Z"}], "created_at"=>"2012-02-10T20:32:14Z", "duration"=>0, "id"=>"4f357ece8af1b04a54000ef2", "updated_at"=>"2012-02-11T17:25:00Z", "action"=>"update", "controller"=>"tasks"}}
MONGODB timetrack_development['tasks'].find({:_id=>BSON::ObjectId('4f357ece8af1b04a54000ef2')}).limit(-1)
MONGODB timetrack_development['tasks'].update({:_id=>BSON::ObjectId('4f357ece8af1b04a54000ef2')}, {"_id"=>BSON::ObjectId('4f357ece8af1b04a54000ef2'), "description"=>"Task with time entry", "duration"=>0, "created_at"=>2012-02-10 20:32:14 UTC, "updated_at"=>2012-02-11 17:25:19 UTC, "time_entries"=>[{"_id"=>BSON::ObjectId('4f36a47f8af1b04a54000ff9'), "task_id"=>nil, "startDate"=>2012-02-11 00:00:00 UTC}]})
请注意,rails 正确获取了我的 startDate 和 endDate 参数作为参数中的 ISO8601 格式字符串,但对 update 的调用将时间片段设置为 00:00:00。我做错了什么?
I'm learning to use Backbone js with Rails and Mongo. Here are my models:
class Task
include MongoMapper::Document
include ActionView::Helpers
many :time_entries
key :description, String
key :duration, Integer, :default => 0
timestamps!
end
and
class TimeEntry
include MongoMapper::EmbeddedDocument
belongs_to :task
key :startDate, Date
key :endDate, Date
end
For some reason, the date times that I send as startDate and endDate have the times dropped when updating mongo, but I can see backbone is pushing them through correctly:
Started PUT "/tasks/4f357ece8af1b04a54000ef2" for 127.0.0.1 at 2012-02-11 12:25:19 -0500
Processing by TasksController#update as JSON
Parameters: {"description"=>"Task with time entry", "time_entries"=>[{"startDate"=>"2012-02-11T17:25:19.529Z"}], "created_at"=>"2012-02-10T20:32:14Z", "duration"=>0, "id"=>"4f357ece8af1b04a54000ef2", "updated_at"=>"2012-02-11T17:25:00Z", "task"=>{"description"=>"Task with time entry", "time_entries"=>[{"startDate"=>"2012-02-11T17:25:19.529Z"}], "created_at"=>"2012-02-10T20:32:14Z", "duration"=>0, "id"=>"4f357ece8af1b04a54000ef2", "updated_at"=>"2012-02-11T17:25:00Z", "action"=>"update", "controller"=>"tasks"}}
MONGODB timetrack_development['tasks'].find({:_id=>BSON::ObjectId('4f357ece8af1b04a54000ef2')}).limit(-1)
MONGODB timetrack_development['tasks'].update({:_id=>BSON::ObjectId('4f357ece8af1b04a54000ef2')}, {"_id"=>BSON::ObjectId('4f357ece8af1b04a54000ef2'), "description"=>"Task with time entry", "duration"=>0, "created_at"=>2012-02-10 20:32:14 UTC, "updated_at"=>2012-02-11 17:25:19 UTC, "time_entries"=>[{"_id"=>BSON::ObjectId('4f36a47f8af1b04a54000ff9'), "task_id"=>nil, "startDate"=>2012-02-11 00:00:00 UTC}]})
Notice that rails is getting my startDate and endDate params correctly as ISO8601 formatted strings in the params, but the call to update sets the time piece to 00:00:00. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在丢失一天中的时间,因为您使用的是
Date
,而您的意思是Time
。 MongoMapper 类型文档几乎说“阅读源代码”,所以我们会这样做;如果您查看extensions/time.rb< /code>
你会发现它适用于 Time 实例,但是如果你看看
extensions/date.rb
您将看到它适用于 Date 实例:因此
startDate
或endDate
上的任何时间在进出 MongoDB 时都会消失。尝试像这样定义您的 TimeEntry:
You're losing the time-of-day because you're using
Date
where you meanTime
. The MongoMapper types documentation pretty much says "read the source" so we'll do that; if you look atextensions/time.rb
you'll see that it works with Time instances but if you look atextensions/date.rb
you'll see that it works with Date instances:So any time-of-day on
startDate
orendDate
goes away on the way in and out of MongoDB.Try defining your TimeEntry like this: