Forgotten :cache_store in Rails documentation

Olexandr avatar

Olexandr

Foreword

The main objective of my article is to share my thought process and demonstrate how I handled the issues that I encountered. Also, It has a message like "do not trust the documentation 100%, sometimes you need to check the source code, because what was forgotten in the documentation cannot be forgotten there"

Facing issue

Once, when I was configuring Rails sessions, I discovered that Rails was storing encrypted session information on the client-side. I wanted to change this so that the client would only store the session ID, and the server would store the session information associated with that ID. However, when I looked at the documentation, I found that there were only four options for session storage: :cookie_store, :mem_cache_store, a custom store, or :disabled. Unfortunately, there was no option for a cache store.

https://guides.rubyonrails.org/configuring.html
https://guides.rubyonrails.org/configuring.html

Then I started investigating where I could find a method to store the session in the cache (I decided that this would be the best solution for my issue).

Investigation

To start my investigation, I looked at the session store sources:

gems/railties-6.1.7.2/lib/rails/application/configuration.rb
gems/railties-6.1.7.2/lib/rails/application/configuration.rb

What I found was:

gems/railties-6.1.7.2/lib/rails/application/configuration.rb
gems/railties-6.1.7.2/lib/rails/application/configuration.rb
  1. If the session store is not defined, we write the first argument to @session_store. If we pass :active_record_store, we also check if the appropriate gem is included in our project's Gemfile.
  2. If the session store is defined, we return a specific class depending on the argument that we pass.

The next step was to look at where the session_store method is called:

gems/railties-6.1.7.2/lib/rails/application/configuration.rb
gems/railties-6.1.7.2/lib/rails/application/configuration.rb
  1. This code shows how Rails sets the default value of :cookie_store for the session store.
  2. This code shows how we set the session store to the Rails middleware.

Next, I revisited the session_store source to look at the 'else' section and find the arguments that are defined in the Rails documentation:

gems/railties-6.1.7.2/lib/rails/application/configuration.rb
gems/railties-6.1.7.2/lib/rails/application/configuration.rb
  1. :disabled - 👍
  2. :active_record_store - 👍
  3. A symbol? But we expected to see at least :cookie_store and :mem_cache_store - 👎

Next, I looked at what ActionDispatch::Session has for us:

gems/railties-6.1.7.2/lib/rails/application/configuration.rb
gems/railties-6.1.7.2/lib/rails/application/configuration.rb

And there they are! We found what we were looking for.

gems/actionpack-6.1.7.2/lib/action_dispatch.rb
gems/actionpack-6.1.7.2/lib/action
  1. :cookie_store - 👍
  2. :mem_cache_store - 👍
  3. :cache_store - 🤔

However, there is no :cache_store argument in documentation! Okay, lets got to feedback section

https://guides.rubyonrails.org/configuring.html
https://guides.rubyonrails.org/configuring.html

Yep, we have found some 🙃

Let's go to contribute to Rails documentation

https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation
https://edgeguides.rubyonrails.org/contributing
https://github.com/rails/rails/tree/main/guides/source
https://github.com/rails/rails/tree/main/guides/source
https://github.com/rails/rails/blob/main/guides/source/configuring.md
https://github.com/rails/rails/blob/main/guides/source/configuring.md

Wait, what? O.o
It actually contains :cache_store now! Someone must have fixed it, but who and when did it happen?

I downloaded the Rails repository and used Git to check who made these changes

rails/guides/source/configuring.md
rails/guides/source/configuring.md
  1. I belive it was fixed on March 28th, 2022 by Ghouse Mohamed.
  2. It definitely seems like it was fixed now 🙃

Why didn't the official Rails documentation include this? It's because the top image shows the main branch, while the documentation is related to the '7.0.4.3' tag which corresponds to the 7-0-stable branch. This branch is missing the commit from the main branch that resolved the issue. My assumption is that the fix from the main branch will be incorporated into the next major release of Rails.

Olexandr avatar
Written By

Olexandr

Enjoyed the post?

Clap to support the author, help others find it, and make your opinion count.