2012-11-08 21 views
1

这两个代码片段之间有什么区别?你应该什么时候使用一个与另一个?将应用的时区设置为当前用户的时区 - Time.zone vs Time.use_zone

Time.zone

class ApplicationController < ActionController::Base 
    before_filter :set_time_zone 

    def set_time_zone 
    Time.zone = current_user.time_zone 
    end 
end 

Time.use_zone

class ApplicationController < ActionController::Base 
    around_filter :set_time_zone 

    def set_time_zone(&block) 
    Time.use_zone(current_user.time_zone, &block) 
    end 
end 

回答

0

好像Time.use_zone覆盖Time.zone本地提供的区块内,然后当它所做的一切重置Time.zone现有价值。

所以第二个代码块是在每个方法的开始调用Time.zone = current_user.time_zone,然后在你的/config/application.rb

指定重置回默认Time.zone虽然我仍然不知道相当于其中推荐做法。从性能角度看,第一种选择似乎更好,但可能会出现第二种选择更有意义的情况。

这里更多: