[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Date manipulation in rails

Johan Allard

12/10/2004 10:13:00 PM

Hi all,

I live in australia in in Australia dates are written as DD/MM/YYYY. In
the database, the dates are stored as YYYY-MM-DD. I want to use the
before_validation function and check if #{(\d+)/(\d+)/(\d+)} and then
convert to the correct iso date format.

The problem is that when the before_validation function kicks in, the
date variable is now a Date object. It is no longer a string, and it
has been formatted using the american MM/DD/YYYY format to YYYY-MM-DD.
Since it is a date object, it is not possible to check how the date was
formatted.

Currently I run the check in the controller and to keep the model
intact it would be fantastic if there was a way of doing this in the
before_validation function (or similar) instead.

Thank you

//johan



1 Answer

Tobias Luetke

12/10/2004 10:34:00 PM

0

You could override the date= method of the model to store away the
string which is assigned to the model in an instance variable so you
can later validate it.

This assumes that you get the date as a string through the parameters.
Did you consider to use
the date helpers of rails like date_select ?

http://api.rubyonrails.org/classes/ActionView/Helpers/DateH...




On Sat, 11 Dec 2004 07:13:01 +0900, Johan Allard <johan@allard.nu> wrote:
> Hi all,
>
> I live in australia in in Australia dates are written as DD/MM/YYYY. In
> the database, the dates are stored as YYYY-MM-DD. I want to use the
> before_validation function and check if #{(\d+)/(\d+)/(\d+)} and then
> convert to the correct iso date format.
>
> The problem is that when the before_validation function kicks in, the
> date variable is now a Date object. It is no longer a string, and it
> has been formatted using the american MM/DD/YYYY format to YYYY-MM-DD.
> Since it is a date object, it is not possible to check how the date was
> formatted.
>
> Currently I run the check in the controller and to keep the model
> intact it would be fantastic if there was a way of doing this in the
> before_validation function (or similar) instead.
>
> Thank you
>
> //johan
>
>


--
Tobi
http://blog.le...