Search code examples
rubyactivesupport

undefined method `zone` for Time:Class after requiring active_support/time_with_zone


I'm on Ruby 2.2.1 and have active_support 4.2 installed so I want to use

Time.zone.parse('2007-02-10 15:30:45')

as described here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

But even after requiring active_support and active_support/time_with_zone, I doesn't seem like Time.zone still doesn't work. Observe:

2.2.1 :002 >   require "active_support"
 => true
2.2.1 :003 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
2.2.1 :004 > require "active_support/time_with_zone"
 => true
2.2.1 :005 > Time.zone
NoMethodError: undefined method `zone' for Time:Class

What's going on?


Solution

  • The zone class method for the Time class is actually in active_support/core_ext/time/zones. You can require that class if you really want to use Time.zone but a better approach might to require active_support/all

    Suggested Solution

    require "active_support/all"
    

    If you want to check out the source code fort for active_support look at the github repo