---
title: "Rails 5.1 doesn't share thread_mattr_accessor variable"
description:
  "Rails 5.1 now does not share the variable defined by thread_mattr_accessor
  with subclass."
canonical_url: "https://www.bigbinary.com/blog/rails-5-1-does-not-share-thread-mattr-accessor-variable-with-sub-class"
markdown_url: "https://www.bigbinary.com/blog/rails-5-1-does-not-share-thread-mattr-accessor-variable-with-sub-class.md"
---

# Rails 5.1 doesn't share thread_mattr_accessor variable

Rails 5.1 now does not share the variable defined by thread_mattr_accessor with
subclass.

- Author: Mohit Natoo
- Published: May 16, 2017
- Categories: Rails 5.1, Rails

Rails 5.0 provides
[mattr_accessor](rails-5-adds-ability-to-create-module-and-class-level-variables-on-per-thread-basis)
to define class level variables on a per thread basis.

However, the variable was getting shared with child classes as well. That meant
when a child class changed value of the variable, then its effect was seen in
the parent class.

```ruby

class Player
  thread_mattr_accessor :alias
end

class PowerPlayer < Player
end

Player.alias = 'Gunner'
PowerPlayer.alias = 'Bomber'

> PowerPlayer.alias
#=> "Bomber"

> Player.alias
#=> "Bomber"

```

This isn't the intended behavior as per OOPS norms.

In Rails 5.1
[this problem was resolved](https://github.com/rails/rails/pull/25681). Now a
change in value of `thread_mattr_accessor` in child class will not affect value
in its parent class.

```ruby

class Player
  thread_mattr_accessor :alias
end

class PowerPlayer < Player
end

Player.alias = 'Gunner'
PowerPlayer.alias = 'Bomber'

> PowerPlayer.alias
#=> "Bomber"

> Player.alias
#=> "Gunner"

```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-1-does-not-share-thread-mattr-accessor-variable-with-sub-class)
