Nil handling in ActiveRecord et al

My database has a column called birthday.

The model has a method:

def age
return (birthday.nil? ? -1 : DateTime.now.year - birthday.year)
end

When called from a validation method on a new record, I get:

NameError in ‘User should be valid’
undefined local variable or method `birthday’ for #User:0x3285e58

My interpretation after some experimenting is that because it’s a new
record and birthday is not initialized, activerecord doesn’t give me
an accessor called birthday.

What is the ‘proper’ way to write the age method?

Thanks,

Pito

On 12/28/07, Pito [email protected] wrote:

My database has a column called birthday.

The model has a method:

def age
return (birthday.nil? ? -1 : DateTime.now.year - birthday.year)

I think you may want self.birthday here.

end


Greg D.
http://destiney.com/