Backbone、Rails 3.1 和 MongoMapper 的日期时间

发布于 2025-01-05 00:07:03 字数 1892 浏览 1 评论 0原文

我正在学习将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2025-01-12 00:07:04

您正在丢失一天中的时间,因为您使用的是 Date,而您的意思是 Time。 MongoMapper 类型文档几乎说“阅读源代码”,所以我们会这样做;如果您查看 extensions/time.rb< /code>你会发现它适用于 Time 实例,但是如果你看看 extensions/date.rb 您将看到它适用于 Date 实例:

def to_mongo(value)
  #...    
  date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
  ::Time.utc(date.year, date.month, date.day)
  #...
end

def from_mongo(value)
  value.to_date if value.present?
end

因此 startDateendDate 上的任何时间在进出 MongoDB 时都会消失。

尝试像这样定义您的 TimeEntry:

class TimeEntry
  include MongoMapper::EmbeddedDocument

  belongs_to :task

  key :startDate, Time
  key :endDate, Time
end

You're losing the time-of-day because you're using Date where you mean Time. The MongoMapper types documentation pretty much says "read the source" so we'll do that; if you look at extensions/time.rb you'll see that it works with Time instances but if you look at extensions/date.rb you'll see that it works with Date instances:

def to_mongo(value)
  #...    
  date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
  ::Time.utc(date.year, date.month, date.day)
  #...
end

def from_mongo(value)
  value.to_date if value.present?
end

So any time-of-day on startDate or endDate goes away on the way in and out of MongoDB.

Try defining your TimeEntry like this:

class TimeEntry
  include MongoMapper::EmbeddedDocument

  belongs_to :task

  key :startDate, Time
  key :endDate, Time
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文