Rails标准日期选择器导致模型中的nil日期值

时间:2011-09-12 03:07:34

标签: ruby-on-rails ruby-on-rails-3 forms mongoid

我确信这很简单,但我遗漏了一些东西。这一直以前都有效。我在Rails 3.1上使用Mongoid。

a = {"title"=>"bad2", "starts_at(1i)"=>"2011", "starts_at(2i)"=>"9", "starts_at(3i)"=>"12", "starts_at(4i)"=>"10", "starts_at(5i)"=>"49", "ends_at(1i)"=>"2011", "ends_at(2i)"=>"9", "ends_at(3i)"=>"12", "ends_at(4i)"=>"11", "ends_at(5i)"=>"49", "all_day"=>"0", "description"=>"foo2"} 
e = Event.new(a)
 => #<Event _id: 4e6d765af11aac06e8000004, _type: nil, created_at: nil, updated_at: nil, title: "bad2", starts_at: nil, ends_at: nil, all_day: false, description: "foo2", starts_at(1i): "2011", starts_at(2i): "9", starts_at(3i): "12", starts_at(4i): "10", starts_at(5i): "49", ends_at(1i): "2011", ends_at(2i): "9", ends_at(3i): "12", ends_at(4i): "11", ends_at(5i): "49"> 
ruby-1.9.2-p290 :020 > e.valid?
 => true 
ruby-1.9.2-p290 :021 > e.save
 => true 
ruby-1.9.2-p290 :022 > e.starts_at
 => nil 
ruby-1.9.2-p290 :023 > e.ends_at
 => nil 

所有相关代码均在此处:https://gist.github.com/1210498

1 个答案:

答案 0 :(得分:5)

使日期助手工作的多参数属性代码来自ActiveRecord,并且最初并未在Mongoid中实现。它现在,但你必须明确要求它。这样那些不使用它的人就不会受到性能损失。

class Event
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes
  ...
end

相关代码如下:
https://github.com/mongoid/mongoid/blob/master/lib/mongoid/multi_parameter_attributes.rb