1

これら2つのコードスニペットの違いは何ですか?そして、いつ一方を使用する必要がありますか?

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
4

1 に答える 1

0

提供されたブロック内でローカルにTime.use_zoneオーバーライドし、完了すると既存の値にリセットするようです。Time.zoneTime.zone

Time.zone = current_user.time_zoneしたがって、2 番目のコード ブロックは、各メソッドの先頭で呼び出してからTime.zone/config/application.rb

どちらが推奨されるアプローチであるかはまだわかりませんが。パフォーマンスの観点からは、最初のオプションの方が優れているように見えますが、2 番目のオプションの方が適切な場合もあります。

詳細はこちら:

于 2013-10-30T23:01:13.700 に答える