Forum: Ruby on Rails Rails, SQL Server 2000 and Dates Pre 1970

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lee I. (Guest)
on 2007-07-26 19:33
(Received via mailing list)
When using dates in Rails with SQL server everything seems to work
great until I attempt to use a date prior to 1970. I can see the date
is correct in the database but when I attempt to read the value it is
returned as nil, if I ask for it again I get the correct date.

I can write dates prior to 1970 into the database however I think is
facilitated by validates_date plugin which seems to work a charm.
Without the plugin I think I would get errors.

If in my controller I ask for the date to be formated as string and
just rescue the failure as it always fails first time, then the date
works from then on, but I would really like to get this fixed so I
dont have to perform this little dance every time I use a date field.

I have found a bug report http://dev.rubyonrails.org/ticket/3430 which
was opened around 2 years ago and there are a couple of suggestions
but none seem to work for me.


Does anyone have any suggestions?
Lee I. (Guest)
on 2007-07-26 19:45
(Received via mailing list)
forgot to add that its rails 1.2.3

Here is the console feedback showing the issue as well.

>> can = Candidate.find(1)
can = Candidate.find(1)
=> #<Candidate:0x4658160 @attributes={"canid"=>"", "minority_id"=>nil,
"adjustment_assessment"=>"", "adjustment_learning"=>"",
"sensory_id"=>nil, "gender"=>"Male", "firstname"=>"B ",
"lastname"=>"Jones", "id"=>1, "assessment_id"=>1,
"note_take_method_id"=>nil, "help_achieve"=>"",
"additional_evidence"=>"", "date_of_birth"=>"1901/04/04 00:00:00"}>
>> can.date_of_birth
can.date_of_birth
=> nil
>> can.date_of_birth
can.date_of_birth
=> #<DateTime: 4830957/2,0,2299161>
>>

Its just plain weird.
Lee I. (Guest)
on 2007-07-26 20:42
(Received via mailing list)
I know I keep talking to myself but here goes nothing.

In the model I have added:

def date_of_birth
    Date.parse(self.date_of_birth_before_type_cast)
end

Which means that the date returns first time, so the problem is
definately to do with the casting of the field. How I fix this I don't
know yet, but at least the workaround seems to do the trick. And I
only need to add it to each model using dates but better than in the
controllers.
This topic is locked and can not be replied to.