# Configure Classes Using the Configurable Concern in Rails

*Published on September 27, 2022*

---


Sometimes, you need a config-like property on a class, such as a logger. Rails provides the&nbsp;`Configurable`&nbsp;concern that lets you accomplish this. It provides a&nbsp;`config`&nbsp;method that allows you to access (read and write) configuration options using the method-like syntax.

```ruby
class Invoice
  include ActiveSupport::Configurable
  
  config_accessor :type
end

Invoice.type # => nil
Invoice.type = :cheque
Invoice.type # => :cheque
```

A nice benefit of using&nbsp;`Configurable`&nbsp;is that all the instances get the same config option, which they can override independently without affecting the class config.

```ruby
invoice = Invoice.new
invoice.type # => :cheque

invoice.type = :credit
invoice.type # => :credit

Invoice.type # => :cheque
new_invoice = Invoice.new
new_invoice.type # => :cheque
```

Behind the scenes, the&nbsp;`config`&nbsp;method returns an instance of&nbsp;`OrderedOptions`&nbsp;class. To learn more, check out my article on ordered options.

[How to Access Hash Values Like Methods using OrderedOptions](/accessing-hash-values-like-methods)

## Practical Use

Rails uses the&nbsp;`Configurable`&nbsp;concern to add&nbsp;`logger`&nbsp;configuration option on the controllers.

The&nbsp;`AbstractController::Base`&nbsp;class includes this concern so you can configure a logger on the controllers.

```ruby
# actionpack/lib/abstract_controller/base.rb

require "active_support/configurable"

module AbstractController
  class Base
    include ActiveSupport::Configurable

    # rest of the code
    end
  end
end
```

Then the&nbsp;`AbstractController::Logger`&nbsp;module (concern) configures the&nbsp;`:logger`&nbsp;config on the controllers.

```ruby
# actionpack/lib/abstract_controller/logger.rb

module AbstractController
  module Logger
    extend ActiveSupport::Concern

    included do
      config_accessor :logger
    end
  end
end
```

This allows you to configure loggers on your controllers, as well as access them whenever needed.

To learn more about concerns and how they work, check out this detailed article.

[Concerns in Rails: Everything You Need to Know](/how-rails-concerns-work-and-how-to-use-them)[  
](/how-rails-concerns-work-and-how-to-use-them)

