On Tue, Jan 11, 2011 at 3:49 AM, Robert K.
[email protected] wrote:
My issue:
From what I’ve been reading, the date format has been changed in
1.9.2. So, saving dates in the “mm/dd/yyy” format has been an issue
since 1.9.2 is saving as “dd/mm/yyyy”
What exactly do you mean by “saving”? Both “mm/dd/yyy” and
“dd/mm/yyyy” are just textual representations of a Date. If you want
to be sure what conversion is used you can use Date#strftime:
I’m pretty sure he’s talking about how Date.parse interprets a string
of the form “1/2/2011”
This form is ambiguous of course, in the US it’s normal meaning would
be January 1, 2011, where in most of the civilized world it would be
February 1, 2011
Date.parse in ruby 1.8.6 tries to use heuristics to decide how to
resolve the ambiguity. Ruby 1.9 (and IIRC 1.8.7 as well) did away with
this and simply interprets such an input string as February 1.
=> “11/01/2011”
=> “Tue, 11 Jan 2011 00:00:00 +0000”
True but probably mostly irrelevant if I’m interpreting the OPs
questions correctly.
I have a jQuery plugin that uses the date picker and displays in the
format of “mm/dd/yyyy” However, sometimes the date saves and other
times it doesn’t because of ruby expecting the changed format (mm/dd/
yyyy). For example:
3/1/2011 saves as 1/3/2011
3/13/2011 doesn’t save (returns “”)
I would expect the date picker to return a Date and not a String.
What am I missing?
The date picker is javascript in a browser, the ‘date’ needs to be
transported via a parameter in a HTTP post, which only allows strings.
So the date needs to arrive at the server in the form of a string,
which then gets parsed into a ruby date, and this is ultimately done
by Date._parse which is the method whose semantics have changed in
Ruby 1.8.7/1.9
I have the following code in my model to inspect the date (in my case,
start_date of a course):
[code]
before_filter :fix_dates
before_filter is a controller method, not a model method isn’t it?
Sorry pure-ruby folks but the OPs question is in the context or Rails.
To fix this I think the OP wants to do the conversion from the string
in the controller, so that it’s already a ruby Date or DateTime by the
time AR sees it. Exactly how to do this depends on the controller
itself. Asuming that the model involved is Foo, and we have a
standard FoosController and a standard form being posted, something
like this:
def create
if (params[:foo] && date_string = params[:foo][:start_date])
params[:start_date] = Date.strptime(date_string), “%m/%d/%Y”)
end
@foo = Foo.create(params[:foo])
if @foo.save
#handle case when foo is valid
else
#handle validation errors
end
end
HTH
–
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale