If you’ve enabled
config.threadsafe! in your Rails production environment, the chances are good that you’ll encounter a confusing “uninitialized constant” error when you try to seed your database using
The fact is Rails’ thread safety mode does not work with
rake db:seed. No need to disable this option entirely, though. With a small modification to your environment and a simple Rake task, you’ll be good to go.
# Enable threaded mode
config.threadsafe! unless ENV['THREADSAFE'] == 'off'
namespace :threadsafe do
# desc 'Enable thread-safe mode (enabled by default in production)'
task :enabled do
# desc 'Disable thread-safe mode'
task :disabled do
ENV['THREADSAFE'] = 'off'
# Ensure we are always running in single-threaded mode for Rake tasks
We’re using an environment variable to tell our application whether we want to enable threaded mode or not, then disabling for all tasks that depend on the Rails environment. You can also specify it manually on the command line if you prefer that instead of making it a dependency for
RAILS_ENV=production rake threadsafe:disabled db:seed).