Thursday, December 11, 2008

Rails multi-param attribute processing for dates

If you're manually handling date parameters built using rails date select helper be careful with Date.new since invalid dates (02/31/2008) will raise an exception. Others have blogged about this but I haven't come across a post mentioning how to deal with invalid dates the rails way just yet.

If you let rails do the work, it will try to convert the invalid date to a valid date for you. Using that code as inspiration, you can do the following:
@birthdate = Time.local(params['birthdate(1i)'].to_i, params['birthdate(2i)'].to_i, params['birthdate(3i)'].to_i).to_date


No comments: