2011-05-27 25 views
3

我一直有這個問題幾天,找不到任何解決方案。看來,我不能改變一個字段的日期(&日期時間)的格式在Mongoid文檔Mongoid Date(DateTime)字段未正確解析以存儲到數據庫

class Project 
    include Mongoid::Document 

    field :deadline, :type => Date 
end 

然後我可以指定日期如下:

p = Project.new 
p.deadline = "20-10-2011" 

但我不能指定的其它格式:

p.deadline = "20/10/2011" 
ArgumentError: invalid date 
    from /Users/pww/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/date.rb:956:in `new_by_frags' 
    from /Users/pww/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/date.rb:1000:in `parse' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/extensions/date/conversions.rb:18:in `convert_to_time' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/extensions/time_conversions.rb:6:in `set' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/field.rb:109:in `set' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/attributes.rb:182:in `typed_value_for' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/attributes.rb:96:in `write_attribute' 
    from /Users/pww/.rvm/gems/[email protected]/gems/mongoid-2.0.2/lib/mongoid/fields.rb:161:in `deadline=' 
    from (irb):11 

事情是我試圖改變Mongoid日期的默認格式在幾個方面,包括

Date::DATE_FORMATS[:default] = "%d/%m/%Y" 

它能夠以該格式顯示數據,但不能以格式存儲數據。我試着用本地化文件如下:

date: 
    formats: 
     default: "%d/%m/%Y" 
     short: "%b %d" 
     long: "%B %d %Y" 

它也不起作用。這可能是我不知道如何正確使用它,但這可能是Mongoid的一個問題。

我使用:

Mongoid (2.0.2) 
Rails (3.0.6) 
ree (1.8.7-2011.03) 

我意識到這一點(https://github.com/mongoid/mongoid/issues/53),這更是一個日期時區的問題。

任何幫助和信息將不勝感激。

謝謝。

回答

3

如果該屬性被定義爲日期,它需要一個有效的Date對象。 您應該負責解析該值並指定日期。

p = Project.new 
p.deadline = Time.Time.strptime("20/10/2011", "%d/%m/%Y") 
+0

嗯...它正常工作與 「20-10-2011」 字符串沒有正確的解析。我知道我可以像你說的那樣手動解析它們。但是必須有一種方法可以將其中一種格式設置爲默認格式,以供Mongoid使用。 – 2011-05-27 14:19:48

1

因爲字段:截止日期,:type =>日期將生成具有時間類型的對象,而不是日期類型。你可以檢查它與 p.deadline.is_a? Date軌控制檯會導致假的,但p.deadline.is_a? Time會產生真實的,

更新您的mongoid到最新版本

gem 'mongoid', :git => "git://github.com/mongoid/mongoid.git" 
2

解決了這個問題其實我已經成功地做到這一點半自動,由通過元編程

#this returns all the Date fields as an array 
    def self.date_fields 
    self.fields.map {|f,v| f if v.type == Date}.compact 

    end 


    def self.convert_dates 

    #go through all the fields and define a method for each 
    self.date_fields.each do |f| 

    define_method "#{f}=".intern do |arg| 

    #if there is a value 
    if arg.present? 
    begin 
    #try to parse it the normal d/m/Y way 
     new_date =Date.parse(arg) 

    rescue 
     #if it fails attempt the US format. Could add more formats by nesting 
     #rescues 
     new_date = DateTime.strptime(arg, '%m/%d/%Y') 
    end 
     #call super to let Mongoid handle it 
    super(new_date) 
    end 
    end 
    end 

的convert_dates會在你的初始化方法被稱爲重新定義日期字段的setter方法(我使用的是自定義類工廠)

0

解決它像這樣:

# our form sends in month, day, year 
    def my_date=(*args) 
    if args.first.is_a?(String) 
     args[0] = Time.strptime(args[0], "%m/%d/%Y") 
    end 
    super(*args) 
    end 
相關問題