Dates not being typecast properly, becoming nil?

I have a rails 2.3.5 app running on ruby 1.8.7 (2008-08-11 patchlevel
72) [i686-darwin9] locally, and everything is fine.

I’m trying to deploy it to my server, running centos 5.4, ruby 1.8.7
(2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby
Enterprise Edition 2010.01.

When I retrieve records via the app, the created_at is nil. I can
pull the same records through script/console and created_at is
properly set as expected. Through the app though, object.created_at
is nil, object.attributes[‘created_at’] is nil, but
object.created_at_before_type_cast returns the proper string from the
database.

Any ideas what’s going on? I’m stumped.

Jeff Emminger wrote:

I have a rails 2.3.5 app running on ruby 1.8.7 (2008-08-11 patchlevel
72) [i686-darwin9] locally, and everything is fine.

I’m trying to deploy it to my server, running centos 5.4, ruby 1.8.7
(2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby
Enterprise Edition 2010.01.

When I retrieve records via the app, the created_at is nil. I can
pull the same records through script/console and created_at is
properly set as expected. Through the app though, object.created_at
is nil, object.attributes[‘created_at’] is nil, but
object.created_at_before_type_cast returns the proper string from the
database.

Any ideas what’s going on? I’m stumped.

What database are you using? MySQL, PosgreSQL, etc.) I don’t see it
listed here.

MySQL 5.0.77 on the server

On Tue, Feb 16, 2010 at 4:18 PM, jemminger [email protected] wrote:

is nil, object.attributes[‘created_at’] is nil, but
object.created_at_before_type_cast returns the proper string from the
database.

Are you started script/console in production mode? When you look at
the production database does this field have a value set? Lastly, can
you provide a code fragment?

-Conrad

Are you started script/console in production mode?

Yes, the console is in the same environment as the app. The app is
running through passenger 2.2.9

When you look at
the production database does this field have a value set?

Yes, it’s fine in the database,

Lastly, can
you provide a code fragment?

In my view:

<%= contact.created_at.to_s(:ymd) %>

Which results in:
ActionView::TemplateError (wrong number of arguments (1 for 0)) on
line… pointing at the line above, because ‘created_at’ is nil. I
know this because changing it to

    <td><%= contact.created_at.to_s(:ymd) rescue

contact.created_at.inspect %>

results in “nil”.

Via console:

[root@server current]# ./script/console staging
Loading staging environment (Rails 2.3.5)

c = Contact.find 368299
=> #<Contact id: 368299, client_id: 12345, first_name: “Matt”,
last_name: “Android”, is_active: 1, created_at: “2009-11-12 20:47:34”,
updated_at: nil, deleted_at: nil>

c.created_at
=> Thu, 12 Nov 2009 20:47:34 UTC +00:00

Yeah, I have both of those lines in my environment.rb too. Commenting
out the first doesn’t seem to help my issue though, but thanks for the
attempt. I’m going to try reverting to Ruby 1.8.6 next.

I thought this might have been a Passenger bug, but I just tried with
Webrick right now and I get the same problem.

Any ideas on what could be different between Rails loading the objects
for the web vs. the console?

Same problem under ruby 1.8.6 on the server.

So far I seem to know:

  • It isn’t the ruby version, as it happens under 1.8.6, 1.8.7 and 1.9

  • It isn’t the webserver, as it happens with Passenger and Webrick

This would suggest it’s my app, yet the exact same app works as
expected on my macbook in both development and test mode. Also the
fact that on the server, I can issue the exact same statement that the
controller is issuing, and I get my datetime fields parsed into ruby
datetimes as expected, yet via the webserver it does not work.

I’m out of ideas.

I don’t know if this is related but I spent half my morning trying to
figure
out why I was getting nil.include? errors for date fields only after
reloading the page (Ruby 1.8.7, Rails 2.3.5). This was the fix (in
env.rb):

#config.time_zone = ‘UTC’

config.active_record.default_timezone = :utc

@ben:

You were right after all! I had in my application controller a line
where I was also setting ActiveRecord::Base.default_timezone.
Removing this fixed it.

Thanks again!

On Tue, Feb 16, 2010 at 7:35 PM, jemminger [email protected] wrote:

Yes, it’s fine in the database,

Lastly, can
you provide a code fragment?

In my view:
<%= contact.created_at.to_s(:ymd) %>

From the Rails 2.3.5 API, the above should be written as follows:

<%= contact.created_at.to_s %>

Perhaps, there’s something within your code that you’re not displaying
here because I wasn’t able to reproduce the error message.
Thus, could you post a complete code fragment that reproduces the
the below error message?

-Conrad

Conrad,

:ymd is a custom format I’ve defined in my environment.rb.

The issue is resolved however; see a few posts above.

Thanks,
Jeff