Datetime returned as string, sometimes


#1

Hi,

I’m using postgresql 8.1, rails 1.0, and I have a strange problem on a
datetime field (timestamp without time zone in postgresql).

As shown below, the field “value” sometimes returns a string, and
sometimes a Time instance, depending of where the value is used…:

  • breakpoint session from inside an instance method of the object:
    value
    => “1996-05-01 00:00:00”
    value.class
    => String

However, as you can see, the value field is of type datetime:

Detail.columns[3]
=> #<ActiveRecord::ConnectionAdapters::Column:0xb7489378 @limit=nil,
@text=false, @null=true, @primary=false, @default=nil,
@type=:datetime, @name=“value”, @number=false>

If I load an instance form the database, I also get a string back:
Detail.find(44).value.class
=> String

  • But if I load the same instance from a console session I get a Time
    instance!

Detail.find(44).value.class
=> Time

I first thought it could come from the way I created the value (
:value => “2005-01-01” or :value => DateTime.parse(“2005-01-01”)), but
that wasn’t confirmed by my tests.

Actually, it seems to depend of the account I use to log into my
application, and the place where I put the breakpoint call! This could
make sense as the data created for the demo account is not treated
with the same code. However, I don’t understand what happens as it
looks in the right table, sees it works on a datatime field, but still
returns a String, depending on where you are in the code…

In short this is what I found out:
demo account + breakpoint in model instance method => string
demo account + breakpoint outside model instance method ( a view eg) =>
Time
normal user account + breakpoint in model instance method => Time

Has anyone experienced a similar thing?

Thanks

Raph


#2

I found the cause of the problem: The class Detail was derived from
another class which had the value column of type text.

Raph