Getting rake db:seed and config.threadsafe! to play nice

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 rake db:seed.

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.

# config/environments/production.rb

# Enable threaded mode
config.threadsafe! unless ENV['THREADSAFE'] == 'off'
# lib/tasks/threadsafe.rake

namespace :threadsafe do
# desc 'Enable thread-safe mode (enabled by default in production)'
task :enabled do

# desc 'Disable thread-safe mode'
task :disabled do

# Ensure we are always running in single-threaded mode for Rake tasks
Rake::Task['environment'].prerequisites.insert(0, 'threadsafe:disabled')

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 :environment (e.g., RAILS_ENV=production rake threadsafe:disabled db:seed).

Like this post? You might also like Coalmine, my centralized error tracking service for your apps. Coalmine captures errors and all kinds of helpful debugging information, notifies you, and makes it all searchable. Check it out!


  1. Thanks for this post!

    I’ve been seeing the same error in other rake tasks including db:migrate ( when I refer to models in the migration). The previous workaround was to actually modify the environment file before and after to comment out the threadsafe line for the migrate, then enable the line for the deploy.

    Suffice to say, this works much better.

  2. I find setting this scheme of setting a system environment variable as a super global a bit heavy.

    Rake already sets a Ruby global: $rails_rake_task

    So, in config/environments/production.rb, just say:

      config.threadsafe! unless $rails_rake_task

    No other magic is required.

  3. Thanks for sharing this… If run in trouble after updated my Application in production.

  4. s/engine_name.downcase/engine_name.underscore/g

  5. Greetings! Verry helpful advice in thіѕ ρarticular article!
    Ιt’ѕ tҺе ⅼittle changes tһɑt will make tҺе biggest
    changes. Μɑny thanks fоr sharing!

    Feel free to surf too mу webseite

  6. Amazing web-site you possess in here

  7. It’s the best time to make some plans for the future
    and it’s time to be happy. I’ve read this post and if
    I could I desire to suggest you few inreresting things or suggestions.
    Maybe you coul write next articles referring to this article.
    I desire to read more things about it!

  8. Awesome blog you have here but I was curious if you knew of any community
    forums that cover the same topics talked about here?
    I’d really love to be a part of group where I can get suggestions from other
    knowledgeable individuals that share the same interest.
    If you have any recommendations, please let me know.

    Appreciate it!

  9. Having read this I thought it was extremely enlightening.
    I appreciate you finding the time and energy to put this information together.
    I once again find myself spending way too much time both
    reading and commenting. But so what, it was
    still worthwhile!

Leave a comment