The Mongoid Date (DateTime) field is not correctly processed for storage in the database

I had this problem for several days and could not find a solution for this. It seems that I cannot change the format of the Date (& DateTime) field in a Mongoid Document

class Project
  include Mongoid::Document

  field :deadline, :type => Date
end

Then I can assign Date as follows:

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

But I can not assign in other formats:

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/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/extensions/date/conversions.rb:18:in `convert_to_time'
    from /Users/pww/.rvm/gems/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/extensions/time_conversions.rb:6:in `set'
    from /Users/pww/.rvm/gems/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/field.rb:109:in `set'
    from /Users/pww/.rvm/gems/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/attributes.rb:182:in `typed_value_for'
    from /Users/pww/.rvm/gems/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/attributes.rb:96:in `write_attribute'
    from /Users/pww/.rvm/gems/ree-1.8.7-2011.03@v3/gems/mongoid-2.0.2/lib/mongoid/fields.rb:161:in `deadline='
    from (irb):11

I am trying to change the default Mongoid Date format in several ways, including

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

which works to display data in this format, but not to store data in a format. I tried with the localization file as follows:

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

This does not work either. I probably don’t know how to do it right, but it can be a problem with the mongoid.

I use:

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

I know about this (https://github.com/mongoid/mongoid/issues/53), which is more relevant to the time zone issue.

.

.

+3
4

, Date. .

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

, setter Date

    #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

_ ( factory)

+2

: ,: type = > , . rails p.deadline.is_a? Date FALSE, p.deadline.is_a? Time true,

, mongoid

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

:

  # 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
0
source

All Articles