HeadlinesBriefing favicon HeadlinesBriefing.com

Rails Key-Value Store for Config Changes

DEV Community •
×

Developers know the pain of urgent configuration changes requiring a full deploy cycle. A Rails tutorial proposes a simple alternative: using ActiveRecord to build a lightweight key-value store directly in the application database. This avoids the commit-build-deploy grind for simple toggles like enabling a holiday sale banner or switching to maintenance mode.

The solution involves creating a `system_settings` table with a unique key and a text value column. The real innovation comes from custom class methods that let you treat the database table like a Ruby hash, using `SystemSetting[:key]` for reads and writes. This clean API works anywhere in the app without complex gems.

For production use, the guide recommends adding Rails caching to prevent a database query on every page load. The cached version fetches settings once, then pulls from memory, with automatic cache busting on updates. This approach offers a flexible, deploy-once configuration system for feature flags, kill switches, and dynamic announcements.