Re: ActiveRecord uses confusing defaults

Stephen Bartholomew
Fri, 08 Sep 2006 12:23:40 -0000

Jeroen Houben wrote:
> s.postcode # => 0
>
> now I would expect this to return nil
>
> s.street # => ''''
> I would expect this to return nil
Varchar columns are mapped to String and Ints are mapped to Integer.  In
ruby, 0, "" and false are not the same as nil (like in PHP for
example).

Therefore, to check if the return of a method is blank, you can do this:

if s.street.blank?  # if street is 0, "" or false
   puts "street is blank"
end

I *think* that rails also has a neat feature where you can do this:

if s.street?
   puts "street is blank"
end

Incidentally, ''street'' is also a method, not a variable so as
far as I
know it would never be nil.

Hope that helps,

Steve