2012-11-08 1 views
1

두 코드 스 니펫의 차이점은 무엇입니까? 그리고 언제 다른 것을 사용해야합니까?앱의 시간대를 현재 사용자의 시간대로 설정 - Time.zone 대 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을 재설정 .

그래서 두 번째 코드 블록은 권장 난 아직 잘 모르겠어요하지만 당신 /config/application.rb

에 지정된 기본 Time.zone로 다시 재설정 각 방법의 시작 부분에 Time.zone = current_user.time_zone를 호출하는 것과 동일 접근. 성능 관점에서 볼 때 첫 번째 옵션이 더 좋을 것 같지만 두 번째 옵션이 더 적합한 경우가있을 수 있습니다. 여기

더 :

관련 문제