[LRUG] How does Rails decide how to parse ambiguous dates?

Murray Steele murray.steele at gmail.com
Wed Jul 27 02:26:40 PDT 2011


On 27 July 2011 09:42, John Winters <john at sinodun.org.uk> wrote:

>
>> But the Rails i18n helpers should look after that for you. So what is it
>> specifically that isn't working?
>>
>
> As far as I can discover, the Rails i18n helpers don't touch the content of
> a field.  I've been doing some web searching and found others with the same
> problem.  I've also dug through the code (as documented in this thread) and
> as far as I can see, there is no code to apply internationalisation to the
> contents of a field.
>
> The text_field code gets its contents by calling
> <attribute_name>_before_type_**cast on the model and putting the result
> straight into the field.  It's already a string by the time the model hands
> it over to the field code.
>
> I'm open to correction if you can find some code which applies formatting
> to the contents of such a field.  I'm very much a novice at Ruby and Rails
> (but quite an experienced software developer).


I'm pretty sure that it's just doing .to_s on whatever the value of
<attribute_name>_before_type_cast is.  So if the object has been saved, that
<attribute_name>_before_type_cast value is probably a Date or Time object
(depending on your database) and if it's not been saved it'll be whatever
the value was that you assigned to <attribute_name> (so a String or Date or
whatever).

In terms of assignment, if it's using Date.parse that's a ruby stdlib method
that is seriously "magic".  The documentation[1] says "The method will
attempt to parse a date from the String using various heuristics" and if you
dig deeper to view the actual code[2], it's pretty hairy stuff (e.g. 1/2/3
gives you 3rd Feb 2001, but 1/2/2003 gives you 1st Feb 2003.. so, yeah,
magic!).

I think, if you want a specific format going into the text field on your
view, you're going to have to specify it directly with a :value =>
<attribute_name>.to_s(:my_format) (where :my_format is a format defined as
Jordi suggested in config/initializers/time.rb) argument to the text-field.
 You may also have to do the same in reverse with your controllers to
"prepare" the string values coming in from your forms, or do something like
Paul suggested with an explicit <attribute_name>= method in your model
(which you said you'd already tried anyway).

The major problem is that rails apparently tries to hide a lot of this
strings being coerced into objects stuff and somewhat inexplicably pushes
most of it down to the db adapter.  So you may get different results
depending on if you are using something like sqlite (everything's a string,
yay!) vs. postgres (a real database).

I hope this helps, or at least, doesn't confuse you more.

Cheers,

Muz

[1]: http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000358
[2]: http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000458
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110727/0075df8a/attachment-0003.html>


More information about the Chat mailing list