---
title:
  "Rails 6.1 allows per environment configuration support for Active Storage"
description:
  "Rails 6.1 allows per environment configuration support for Active Storage"
canonical_url: "https://www.bigbinary.com/blog/rails-6-1-allows-per-environment-configuration-support-for-active-storage"
markdown_url: "https://www.bigbinary.com/blog/rails-6-1-allows-per-environment-configuration-support-for-active-storage.md"
---

# Rails 6.1 allows per environment configuration support for Active Storage

Rails 6.1 allows per environment configuration support for Active Storage

- Author: Shashank
- Published: January 20, 2021
- Categories: Rails, Rails 6.1

Rails 6.1 allows environment-specific configuration files to set up Active
Storage.

In development, the `config/storage/development.yml` file will take precedence
over the `config/storage.yml` file. Similarly, in production, the
`config/storage/production.yml` file will take precedence.

If an environment-specific configuration is not present, Rails will fall back to
the configuration declared in `config/storage.yml`.

## Why was it needed?

Before Rails 6.1, all storage services were defined in one file, each
environment could set its preferred service in `config.active_storage.service`,
and that service would be used for all attachments.

Now we can override the default application-wide storage service for any
attachment, like this:

```ruby
class User < ApplicationModel
  has_one_attached :avatar, service: :amazon_s3
end
```

And we can declare a custom `amazon_s3` service in the `config/storage.yml`
file:

```ruby
amazon_s3:
  service: S3
  bucket: "..."
  access_key_id: "..."
  secret_access_key: "..."
```

But we are still using the same service for storing avatars in both production
and development environments.

To use a separate service per environment, Rails allows the creation of
configuration files for each.

## How do we do that?

Let's change the service to something more generic in the User model:

```ruby
class User < ApplicationModel
  has_one_attached :avatar, service: :store_avatars
end
```

And add some environment configurations:

For production we'll add `config/storage/production.yml`:

```ruby
store_avatars:
  service: S3
  bucket: "..."
  access_key_id: "..."
  secret_access_key: "..."
```

And for development we'll add `config/storage/development.yml`:

```ruby
store_avatars:
  service: Disk
  root: <%= Rails.root.join("storage") %>
```

This will ensure that Rails will store the avatars differently per environment.

Check out the [pull request](https://github.com/rails/rails/pull/40294) to learn
more.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-1-allows-per-environment-configuration-support-for-active-storage)
