---
title: "Rails 5.2 specifying default value for class_attribute"
description:
  "Rails 5.2 now supports specifying a default value for the class attributes
  defined using `class_attribute`."
canonical_url: "https://www.bigbinary.com/blog/rails-5-2-supports-specifying-default-value-for-a-class_attribute"
markdown_url: "https://www.bigbinary.com/blog/rails-5-2-supports-specifying-default-value-for-a-class_attribute.md"
---

# Rails 5.2 specifying default value for class_attribute

Rails 5.2 now supports specifying a default value for the class attributes
defined using `class_attribute`.

- Author: Vishal Telangre
- Published: February 21, 2018
- Categories: Rails 5.2, Rails

It is very common to set a default value for a `class_attribute`.

Before Rails 5.2, to specify a default value for a `class_attribute`, we needed
to write like this.

```ruby
class ActivityLogger
class_attribute :logger
class_attribute :settings

self.logger = Logger.new(STDOUT)
self.settings = {}
end
```

As we can see above, it requires additional keystrokes to set a default value
for each `class_attribute`.

Rails 5.2 has added support for specifying a default value for a
`class_attribute` using `default` option.

```ruby
class ActivityLogger
class_attribute :logger, default: Logger.new(STDOUT)
class_attribute :settings, default: {}
end
```

This enhancement was introduced in this
[pull request](https://github.com/rails/rails/pull/29270).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-2-supports-specifying-default-value-for-a-class_attribute)
