Skip to content

Exception in db_time_now from Time.zone.now when default_timezone is :local (ie no zone) #11

Open
gregclar opened this Issue Oct 26, 2009 · 8 comments

3 participants

@gregclar

Delayed::Job

def self.db_time_now
  (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
end

App is in rails 2.2.3 on ruby 1.8.7.

[apphome]/config/environments does not set config.time_zone.

error demo'd in console:

> ruby script/console 
Loading development environment (Rails 2.2.3)
>> ActiveRecord::Base.default_timezone
=> :local
>> Time.zone.now
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now
        from (irb):2

Simple work-around: use Time.now instead of Time.zone.now, which works for my Rails settings, but not sure that is a general solution.

@gregclar

delayed_job is from tobi-delayed_job-8df6837

@gregclar

Thanks kossnocorp. That works well.

@dedomenon

Is this really working? With rails 2.3.4 I get an exception when no timezone is set:

(ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now
from (irb):1

My timesone is not set, and so defaults to local:

ActiveRecord::Base.default_timezone
=> :local

The funny thing is that the code exectuted after the test fails:

Time.zone.now
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now
from (irb):3

and that the other branch works:

Time.now.utc
=> Thu Dec 10 15:43:48 UTC 2009

@dedomenon

Trying another formatting....

Is this really working? With rails 2.3.4 I get an exception when no timezone is set:

(ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now
from (irb):1

My timesone is not set, and so defaults to local:

ActiveRecord::Base.default_timezone
=> :local

The funny thing is that the code exectuted after the test fails:

Time.zone.now
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now
from (irb):3

and that the other branch works:

Time.now.utc
=> Thu Dec 10 15:43:48 UTC 2009

@gregclar

Is this really working?

Yes.

With rails 2.3.4 I get....

I'm using rails 2.2.3, but your description of the error and the error message matches my experience.

 script/console 
 Loading development environment (Rails 2.2.3)
 >> (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.zone.now
 NoMethodError: You have a nil object when you didn't expect it!
 The error occurred while evaluating nil.now
         from (irb):1
 >> (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.now
 => Fri Dec 11 10:41:09 +1100 2009

Greg

@dedomenon

Hmm, I still had the old code in place....
Any chance to have this fix integrated in tobi's repo?

@dedomenon

Finally I fixed it by adapting the method causing trouble in an initializer.
You can find it here:
http://github.com/dedomenon/dedomenon/blob/master/config/initializers/delayed_job_config.rb#L1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.