# Where to run cookie rotator when upgrading to Rails 7?

**URL:** <https://discuss.rubyonrails.org/t/where-to-run-cookie-rotator-when-upgrading-to-rails-7/80236>\
**Category:** rubyonrails-talk\
**Tags:** actiondispatch\
**Created:** [March 11, 2022, 3:53pm UTC](https://discuss.rubyonrails.org/t/where-to-run-cookie-rotator-when-upgrading-to-rails-7/80236 "2022-03-11T15:53:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdubois](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@sdubois](https://discuss.rubyonrails.org/u/sdubois)\
**Post date:** [March 11, 2022, 3:53pm UTC](https://discuss.rubyonrails.org/t/where-to-run-cookie-rotator-when-upgrading-to-rails-7/80236/1 "2022-03-11T15:53:58Z")

</div>

The Rails 7 `new_framework_defaults_7_0.rb` file [instructs](https://github.com/rails/rails/commit/c7fca843a5b8b7a16acd93cf7adbe48c29703470) to add some code for registering a cookie rotator (below). Should this code be executed a single time e.g. in a one-off rake task, or executed in an initializer at each application boot? (And in the latter case should it be enclosed in an after\_initialize block as suggested [here](https://github.com/rails/rails/pull/44047)?)

```ruby
Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
  salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
  secret_key_base = Rails.application.secrets.secret_key_base
  key_generator = ActiveSupport::KeyGenerator.new(
    secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1
  )
  key_len = ActiveSupport::MessageEncryptor.key_len
  secret = key_generator.generate_key(salt, key_len)
  cookies.rotate :encrypted, secret
end

```

Question also posted on [StackOverflow](https://stackoverflow.com/questions/71464401/upgrading-to-rails-7-where-to-register-the-cookie-rotator).
