Topic: Choking on nil values and malformed dates

I've got an old database with tables containing rows which have bad dates, eg: "1999-00-00".

When I try and pull these out and either display them on a page or put them into a form, Rails chokes and gives me that "nil object where I didn't expect it!" error.

It also does this if there is a missing one-to-many foreign key.

How can I tell Rails to expect a possible nil object?

Re: Choking on nil values and malformed dates

Typically, in your view you might do:

<%= @person.birthday unless @person.birthday.nil? %>

That can get a bit tedious, but the only other option is:

<%= @person.birthday rescue '' %>

Does this help?

Re: Choking on nil values and malformed dates

cwd wrote:

Does this help?

Yep, it does. Many thanks! This will get me through some early testing.

For production, I may do a find_by_sql and just format the date (eg SELECT YEAR(bad-date)) on the database end.