Prompt
I'm building feature flags into an existing Rails app, how should I do it?
Latest observation
A solid way to add feature flags to an existing Rails app is to treat them as a small, centralized system rather than sprinkling if statements everywhere.
Recommended approach
1) Start with a clear flag model
Decide what kinds of flags you need:
- Boolean flags: on/off
- Percentage rollouts: expose to X% of users
- Targeted flags: enable for specific users/accounts/orgs
- Environment flags: only in staging, etc.
For most Rails apps, start with boolean + targeted + percentage rollout.
2) Use a dedicated feature flag layer
You can build your own or use a gem/service.
Good options
- Flipper: very popular for Rails, flexible, can use DB/Redis, supports percentage and targeting
- Unleash: good if you want a server-based flag system
- LaunchDarkly: enterprise-grade managed solution
If you want something simple and Rails-native, Flipper is usually the best starting point.
3) Keep checks out of controllers/models where possible
Don’t scatter flag logic everywhere. Create a small abstraction:
if Feature.enabled?(:new_checkout, current_user)
render :new_checkout
else
render :old_checkout
end
Wrap access in a service/module like:
module Feature
def self.enabled?(name, actor = nil)
Flipper.enabled?(name, actor)
end
end
This makes it easier to swap implementations later.
4) Use “actors” for targeting
Flags usually need context:
- current user
- account/org
- team
- request environment
With Flipper, you can check by actor:
Flipper.enabled?(:beta_dashboard, current_user)
If your app is multi-tenant, often the account is the better actor than user.
5) Store flag config centrally
You generally want flags persisted in:
- DB for admin-managed flags and auditability
- Redis if you need very fast checks or ephemeral state
- Remote flag service if you want ops/admin UI and advanced rollout controls
For many apps:
- DB-backed Flipper is enough
- add Redis caching if needed
6) Build an admin UI if non-devs need to manage flags
If product/support teams will use them, give them a basic interface to:
- enable/disable flags
- target users/accounts
- set rollout percentages
- see descriptions and owners
At minimum, keep metadata:
- name
- description
- owner
- created_at
- expiry/review date
7) Use flags with a lifecycle
Feature flags should not be permanent.
Process:
- Create flag
- Launch behind flag
- Gradually roll out
- Remove old code once fully launched
- Delete flag
This matters because stale flags make code harder to maintain.
8) Be careful with where you evaluate flags
Do:
- evaluate at the edge of the request
- cache flag state if needed
- keep flags deterministic for a request
Avoid:
- checking flags deep inside loops repeatedly
- using flags in business-critical logic without thinking about consistency
9) Test both paths
For every flag-enabled feature, test:
- flag on
- flag off
- rollout/targeting edge cases
Example:
allow(Feature).to receive(:enabled?).with(:new_checkout, user).and_return(true)
Or test with Flipper directly if using it.
10) Plan for safe defaults
If the flag system fails, decide what should happen:
- fail closed = feature off
- fail open = feature on
Usually for risky features, fail closed is safer.
If you want a practical Rails stack
A common setup is:
- Flipper
- ActiveRecord adapter
- optional Redis cache
- a small
Featurewrapper - admin UI for internal users only
Example install:
# Gemfile
gem "flipper"
gem "flipper-active_record"
gem "flipper-ui"
Then configure Flipper in an initializer and use it throughout the app.
Suggested implementation pattern
app/lib/feature.rb
class Feature
def self.enabled?(name, actor = nil)
Flipper.enabled?(name, actor)
end
end
In controller/view
if Feature.enabled?(:new_nav, current_user)
render "new_nav"
else
render "old_nav"
end
For a rollout
Flipper.enable_percentage_of_actors(:new_nav, 25)
A few best practices
- Name flags clearly:
new_checkout, notflag_123 - Add an owner and expiry date
- Remove flags after rollout
- Don’t use flags to avoid proper product decisions
- Don’t create “flag explosion”
- Consider an audit log for changes
My recommendation
If you’re starting fresh in an existing Rails app:
- Use Flipper
- Back it with ActiveRecord
- Add a thin
Featurewrapper - Start with a few flags only
- Add an internal admin UI later if needed
- Set a process for cleanup
If you want, I can give you:
- a step-by-step Flipper setup for Rails,
- a database-backed example,
- or a migration strategy for adding flags to an existing app safely.