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.
.
.