Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method `getlocal' for DateTime object

When a user logs into my site, Devise tries to update the field in the user model current_sign_in_at with the time. But it's producing this error:

NoMethodError: undefined method `getlocal' for Mon, 08 Nov 2010 03:11:01 +0000:DateTime
[GEM_ROOT]/gems/activesupport-3.0.1/lib/active_support/time_with_zone.rb:75

I've found a couple of resources where people have mentioned this problem. There's an issue on Github with no answer. This page has a solution which works in my development environment, because it involves editing /lib/active_support/time_with_zone.rb, but I need a solution that will also work in my production environment on Heroku. Is there a way I can use my edited version of /lib/active_support/time_with_zone.rb on Heroku? Or is there a better way to deal with the problem?

Here's what I'm using:

Rails 3.0.1
ruby 1.9.2p0 (2010-08-18 revision 29036)

Thanks for reading.

like image 429
ben Avatar asked Feb 16 '26 02:02

ben


2 Answers

 irb> Time.now.getlocal
 => Mon Nov 08 15:04:05 +0200 2010 

but

irb>DateTime.now.getlocal
NoMethodError: undefined method `getlocal' for Mon, 08 Nov 2010 15:05:16 +0200:DateTime
from (irb):17

So, I assume, you need to convert your DateTime object to Time

Updated

You can use Ruby mixin technique, something like

irb > module DateTimePatch
irb ?>  def get_local
irb ?>    "works!"
irb ?>    end
irb ?>  end
 => nil 
irb > DateTime.send(:include, DateTimePatch)
 => DateTime 
irb2 > DateTime.now.get_local
 => "works!" 
like image 109
Sergey Avatar answered Feb 17 '26 15:02

Sergey


I know this question was 2 years ago, but I think I found the clearer explanation about why it behave that way, found on this question. Just for anyone who probably need more explanations like me.

like image 21
ksugiarto Avatar answered Feb 17 '26 14:02

ksugiarto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!