ActiveSupport::CurrentAttributes
Thread.currentは絶対に使ってはいけない
ohbarye.icon PumaとRailsのbugでスレッドセーフでない問題が過去にあったため、利用が躊躇されている? 使い方
Currentモデルを定義し、ストアしたいattributeを宣言する
あとはあらゆるところでCurrent.userに値をセットしたり参照したりできる
code:ruby
# app/models/current.rb
class Current < ActiveSupport::CurrentAttributes
attribute :account, :user
attribute :request_id, :user_agent, :ip_address
resets { Time.zone = nil }
def user=(user)
super
self.account = user.account
Time.zone = user.time_zone
end
end
関連
マルチテナントアプリケーションでリクエストのサブドメインからorganizationを特定して保存する例