satya
Thu Jan 09 2025
ActiveSupport::CurrentAttributes
provides a thread-isolated attributes singleton, perfect for request-specific data like current user, role, or locale.create a
current.rb
in models directory and add the attributes we want to set
class Current < ActiveSupport::CurrentAttributes
attribute :role
end
then in application controller we can set it like this
class ApplicationController < ActionController::Base
before_action :set_current_attributes
def set_current_attributes
Current.role = session[:role]
// ... other attributes
end
end
now in your application we can directly access and use like
Current.role
#CU6U0R822 #rails-current-attributes