---
title: "Rails 5 Create module and class level variables"
description:
  "In Rails 5, we can create specific class and module variables that live for
  the lifetime of a thread"
canonical_url: "https://www.bigbinary.com/blog/rails-5-adds-ability-to-create-module-and-class-level-variables-on-per-thread-basis"
markdown_url: "https://www.bigbinary.com/blog/rails-5-adds-ability-to-create-module-and-class-level-variables-on-per-thread-basis.md"
---

# Rails 5 Create module and class level variables

In Rails 5, we can create specific class and module variables that live for the
lifetime of a thread

- Author: Abhishek Jain
- Published: September 5, 2016
- Categories: Rails 5, Rails

Rails already provides methods for creating class level and module level
variables in the form of
[cattr*\* and mattr*\* suite of methods](http://guides.rubyonrails.org/active_support_core_extensions.html#cattr-reader-cattr-writer-and-cattr-accessor).

In Rails 5, we can go a step further and create
[thread specific class or module level variables](https://github.com/rails/rails/pull/22630).

Here is an example which demonstrates an example on how to use it.

```ruby

module CurrentScope
  thread_mattr_accessor :user_permissions
end

class ApplicationController < ActionController::Base

  before_action :set_permissions

  def set_permissions
    user = User.find(params[:user_id])
    CurrentScope.user_permissions = user.permissions
  end

end

```

Now `CurrentScope.user_permissions` will be available till the lifetime of
currently executing thread and all the code after this point can use this
variable.

For example, we can access this variable in any of the models without explicitly
passing `current_user` from the controller.

```ruby

class BookingsController < ApplicationController
  def create
    Booking.create(booking_params)
  end
end

class Booking < ApplicationRecord
  validate :check_permissions

  private

  def check_permissions
    unless CurrentScope.user_permissions.include?(:create_booking)
      self.errors.add(:base, "Not permitted to allow creation of booking")
    end
  end
end

```

It internally uses
[Thread.current#[]=](https://ruby-doc.org/core-2.3.0/Thread.html#method-i-5B-5D-3D)
method, so all the variables are scoped to the thread currently executing. It
will also take care of namespacing these variables per class or module so that
`CurrentScope.user_permissions` and `RequestScope.user_permissions` will not
conflict with each other.

If you have used
[PerThreadRegistry](http://api.rubyonrails.org/classes/ActiveSupport/PerThreadRegistry.html)
before for managing global variables, `thread_mattr_*` & `thread_cattr_*`
methods can be used in place of it starting from Rails 5.

Globals are generally bad and should be avoided but this change provides nicer
API if you want to fiddle with them anyway!

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-adds-ability-to-create-module-and-class-level-variables-on-per-thread-basis)
